How do I find the Client ID of control within an ASP.NET GridView?

后端 未结 6 1303
旧时难觅i
旧时难觅i 2020-12-03 14:22

I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like to obtain it\'s ID for use in javascript. Something like this:



        
相关标签:
6条回答
  • 2020-12-03 14:33

    I just do this...

    var tbl = document.getElementById('<%=GridView.ClientID%>');
    var checkBox = tbl.rows[i].cells[11].getElementsByTagName("input")[0].id;
    

    the cell should always be the same and it gets rendered into an input. You may have to change the number at the end if you have more then one input in that cell. This will give you the new clientid/id of the input object (checkbox or whatever)

    0 讨论(0)
  • 2020-12-03 14:38

    Try this:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="textDateSent" runat="server">
            </asp:TextBox>                      
           <input type="button" value='Today' onclick="setToday('<%# ((GridViewRow)Container).FindControl("textDateSent").ClientID %>');" /> 
        </ItemTemplate>
    </asp:TemplateField>
    
    0 讨论(0)
  • 2020-12-03 14:38

    Change <%# textDateSent.ClientID %> to <%= textDateSent.ClientID %>.

    Argh, you may need to use the OnDataBinding event of the grid view. Then put a literal control in your javascript. Then you can get the clientID of the text box and feed that into your literal control.

    protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Create an instance of the datarow
                DataRowView rowData = (DataRowView)e.Row.DataItem;
    
                //locate your text box
                //locate your literal control
                //insert the clientID of the textbox into the literal control
            }
        }
    

    Look here for a great detailed tutorial on working within this context.

    0 讨论(0)
  • 2020-12-03 14:43

    Maybe you don't want to do it where you need the ClientID. Check out this post here where the controls in a row are referenced in a generic way.

    0 讨论(0)
  • 2020-12-03 14:48

    You can get client id like this:

    protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strClientID = ((TextBox)e.Row.FindControl("txtName")).ClientID;
        }
    }
    

    This will give unique client ID for each textbox in all rows.

    0 讨论(0)
  • 2020-12-03 14:53

    This is what I did. In the aspx page I just passed the entire object to the javascript function, so I didn't even meed to client id. In my case the object was a drop down list in the EditItemTemplate of the GridView. I added an html onchange(this) event in the aspx code.

    <asp:DropDownList ID="custReqRegionsDDL" runat="server" onchange='custReqRegionsDDLOnChange(this)'> </asp:DropDownList>

    here is my javascript

    function custReqRegionsDDLOnChange(myDDL)
        {
            alert('selected text=' + myDDL.options[myDDL.selectedIndex].text);
    

    }

    0 讨论(0)
提交回复
热议问题