c# - 在 GridView 的 TemplateField 中获取 Html 输入的值

标签 c# asp.net

在下面的示例中,我无法获取 tel 输入“Contract”的值。我对 DropDownList 没有任何问题。

aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="Serial,Model" DataSourceID="ObjectDataSource1" Width="50%" GridLines="None" CellSpacing="-1">
                    <Columns>
                        <asp:BoundField DataField="Serial" HeaderText="Serial" />
                        <asp:BoundField DataField="Model" HeaderText="Model"/>
                        <asp:TemplateField HeaderText="Status">
                        <ItemTemplate>
                            <asp:DropDownList ID="Status" CssClass="dropdownlist" runat="server" onfocus="setSelectedRow(this)" DataValueField="Status" SelectedValue='<%# Bind("Status") %>' onChange="statusChange(this)">
                                <asp:ListItem Value="RENT READY">Rent Ready</asp:ListItem>
                                <asp:ListItem Value="ON RENT">On Rent</asp:ListItem>
                                <asp:ListItem Value="OOS">Out of Service</asp:ListItem>
                            </asp:DropDownList>                               
                            <input type="tel" id="Contract" onfocus="setSelectedRow(this)" value= '<%# Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON RENT" ? "textBox" : "textBoxHidden" %>'></input>
                        </ItemTemplate>
                    </asp:TemplateField>
</asp:GridView>

呈现的 HTML:

enter image description here

到目前为止我尝试了什么:

protected void submit_Click(object sender, EventArgs e)
{
   if (!Page.IsValid) return;
   foreach(GridViewRow row in GridView1.Rows)
   {
      string Serial = row.Cells[0].Text;
      string Model = row.Cells[1].Text;
      string Status = ((DropDownList)row.FindControl("Status")).SelectedValue;
      string Contract =  Any one of the below attempts...

      Sql stored procedure integrating the data back to the server...
   }
}

 string Contract = ((HtmlInputControl)row.FindControl("Contract")).Value;

 string Contract = ((HtmlInputText)row.FindControl("Contract")).Value;

 string Contract = ((TextBox)row.FindControl("Contract")).Text;

所有抛出 Object Reference Not Set to Instance of Object 异常。

最佳答案

像这样更改您的输入:添加一个 runat="server" 属性

aspx文件中使用type = "text"

 <input type="text" runat="server" id="Contract" onfocus="setSelectedRow(this)" value= '<%#   
     Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON  
     RENT" ? "textBox" : "textBoxHidden" %>' ></input>

然后像这样在 OnRowCreated 事件中创建 GridRows 时,在后面的代码中将其更改为 type = "tel"

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs {
        if (e.Row.RowType == DataControlRowType.DataRow)
            ((HtmlInputText)(e.Row.FindControl("Contract")))
                .Attributes.Add("type", "tel");
    }  

使用这个检索输入值

string Contract = ((HtmlInputText)row.FindControl("Contract")).Value;

关于c# - 在 GridView 的 TemplateField 中获取 Html 输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983751/

相关文章:

c# - 如何模拟硬件来测量 .net 应用程序性能?

c# - IEnumerable<T>.Count 在哪些情况下进行了优化?

asp.net - 发送 HTTP header 后,服务器无法附加 header ?

asp.net mvc 5 捆绑 jquery 不工作

c# - Visual Studio 2013 中的浏览器链接

c# - Rhino Mocks 的 "requires a return value or an exception to throw"是什么意思?

c# - FileStream Write() 方法仅写入未知符号

c# - 禁用页面上的多个按钮控件

asp.net - 用户登录时如何比较这个加盐密码?

c# - Helper 方法中的 <Link> 给出 "Element link cannot be nested within element ' 链接'"