在Gridview中查找Html输入

时间:2015-02-06 09:41:50

标签: asp.net gridview

我想在Gridview内找到HTML输入文字 但是我收到了一个错误。

错误

Object reference not set to an instance of an object.

源代码

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="ID" ItemStyle-Width="2%">
            <ItemTemplate>
                <input id="txtNum" runat="server" value='<%#Eval("num") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后

 HtmlInputText txt1 = (HtmlInputText)gridview1.FindControl("txtNum");
 txt1.Attributes["type"] = "tel";

2 个答案:

答案 0 :(得分:0)

需要在gridview的每一行中找到HtmlInputText

<强>代码

if (gridview1.Rows.Count > 0)
{
    for (int i = 0; i < gridview1.Rows.Count; i++)
    {
        HtmlInputControl txt1 = (HtmlInputControl)gridview1.Rows[i].Cells[0].FindControl("txtNum");
        txt1.Attributes["type"] = "tel";
    }
}

答案 1 :(得分:0)

对于未来,我是这样解决的:

--- HTML代码---

 <asp:TemplateField Visible="False" ItemStyle-Width="100%">  
                  <ItemTemplate>

<div class="input-group flex-nowrap">
  <div class="input-group-prepend">
    <span class="input-group-text" id="addon-wrapping">A</span>
  </div>
 <input id="txtMat" type="text" style="width: 100px;" class="form-control" runat="server" onkeypress="return isNumberKey(event);"  data-toggle="tooltip" data-placement="top"  placeholder="Matricula" aria-label="Matricula" aria-describedby="basic-addon1" onkeydown="limit(this, 8);" onkeyup="limit(this, 8);"  min="8"/>
</div>
                     </ItemTemplate>
                    </asp:TemplateField>   </strike>

--- C#----

//我需要使用griview中的输入文本发送电子邮件。在输入文本中,键入电子邮件地址。

protected void GridView_Cat_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

HtmlInputText Matricula = GridView_Cat.Rows[e.RowIndex].FindControl("txtMat") as HtmlInputText;

String address= Matricula.Value;
}

您将需要:

using System.Web.UI.HtmlControls;