在GridView的TemplateField中获取Html输入的值

时间:2012-06-11 16:06:21

标签: 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;

所有抛出对象引用未设置为对象实例异常。

2 个答案:

答案 0 :(得分:2)

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

type = "text"文件中使用aspx

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

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

    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;

答案 1 :(得分:0)

  1. 您需要添加runat="server"才能访问html输入控件 服务器端
  2. value属性没有=符号,并且在呈现的html
  3. 中为空
  4. 呈现的html
  5. 中没有结束标记/

    渲染输入标记:

    <input type="tel" id="Contract" onfocus="setSelectedRow(this)" value placeholder="Contract #" class="textBoxHidden">
    

    你需要这样的东西:

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

    如果value属性仍然遗漏=,请在渲染的html中签名,然后尝试将其值设置为某个虚拟电话。

    value='<%# (string)Eval("Contract") == "" ? "0" : Eval("Contract") %>'