Getting the control id of a control inside gridview control

Last post 05-16-2008 1:13 AM by darkcat02. 10 replies.

Sort Posts:

  • Getting the control id of a control inside gridview control

    05-06-2008, 4:59 AM
    • Loading...
    • darkcat02
    • Joined on 05-05-2008, 10:27 PM
    • Posts 45

     uhmm.. a little bit confusing regarding my subject because i dont know how to say it...

    by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??

     hoping i could get a reply as soon as possible...
     

  • Re: Getting the control id of a control inside gridview control

    05-06-2008, 5:13 AM
    • Loading...
    • kamii47
    • Joined on 05-26-2005, 4:04 PM
    • Karachi, Pakistan
    • Posts 1,520

    What do you want's to do by getting the id of the Text box?

    Do you want's to find particular Textbox with certain id ?

    Kamran Shahid(MCSD.NET,MCPD.net[web])
    Sr. Software Engineer
    Netprosys Inc.
    www.netprosys.com

    Remember to click "Mark as Answer" on the post that helps U
  • Re: Getting the control id of a control inside gridview control

    05-06-2008, 5:16 AM
    • Loading...
    • rbansal44
    • Joined on 12-11-2007, 10:09 AM
    • Posts 100

     hi,

    you can use the find control method....if suppose the textbox is in the first column

    TextBox txt = Gridview1.Rows[0].Cells[0].FindControl("Textboxname") as TextBox;

    thanks,

    rajiv

  • Re: Getting the control id of a control inside gridview control

    05-06-2008, 5:16 AM

    You can do so by using row data bound event and e.row.findcontrol("textbox1").ClientID, where textbox1 is the id of the textbox

  • Re: Getting the control id of a control inside gridview control

    05-06-2008, 5:21 AM

    I agree wihth Kamran Shahid. What do you want to do by getting id?

    Sridhar
  • Re: Getting the control id of a control inside gridview control

    05-06-2008, 5:28 AM
    • Loading...
    • darkcat02
    • Joined on 05-05-2008, 10:27 PM
    • Posts 45

    if i get the id i will put some value into it... is that possible...

    for some reasons... instead of putting codes into the client side.. i would put it to the server side...

    but i cant get the id of it... Tongue Tied 

  • Re: Getting the control id of a control inside gridview control

    05-07-2008, 11:24 PM
    Answer

    Hi darkcat02 ,

    See my sample,

     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("test");
                DataRow dr = table.NewRow();
                dr["test"] = "before change";
                table.Rows.Add(dr);
                this.GridView1.DataSource = table;
                GridView1.DataBind();
    
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           TextBox box =  this.GridView1.Rows[0].FindControl("TextBox1") as TextBox;
           box.Text = "changed";
        }
        string clientid;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox box = e.Row.FindControl("TextBox1") as TextBox;
                clientid = box.ClientID;
            }
        }
        protected void Button2_PreRender(object sender, EventArgs e)
        {
            this.Button2.Attributes.Add("onclick","document.getElementById('"+clientid+"').value = 'changed' ; return false;");
        }

     

        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="194px" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("test") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        
        </div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="change" />
            <asp:Button ID="Button2" runat="server" OnPreRender="Button2_PreRender" Text="change on client" />
        </form>

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • GETTING THE CONTROL ID OF DROPDOWNLIST AND SQLDATASOURCE INSIDE A GRIDVIEW ITEMTEMPLATE

    05-15-2008, 2:18 AM
    • Loading...
    • darkcat02
    • Joined on 05-05-2008, 10:27 PM
    • Posts 45

     thank you for the replies...

     

    but now i still dont get it... sorry... the code isn't getting into my head.. argh!

    i have a simple for you guys... by the way the code above was being asked by my friend... so now.. this question is mine..

     

    i need the code to find the control id (again! yes.. again! i cant understand how should i do it) of a dropdownlist and sqldatasource... how should i do it...

     pls help me... i am still a student... anyone? Sad
     

  • Re: GETTING THE CONTROL ID OF DROPDOWNLIST AND SQLDATASOURCE INSIDE A GRIDVIEW ITEMTEMPLATE

    05-16-2008, 12:43 AM
    • Loading...
    • ams16
    • Joined on 05-12-2008, 9:48 AM
    • Posts 60

     

    on  itemdatabound function of gridview

    just give the following code;

    TextBox objtxt1 = (TextBox)e.Item.FindControl("txt1");

    string  strtxtID = objtxt1.ClientID;

  • Re: GETTING THE CONTROL ID OF DROPDOWNLIST AND SQLDATASOURCE INSIDE A GRIDVIEW ITEMTEMPLATE

    05-16-2008, 1:00 AM
    • Loading...
    • chandan.max
    • Joined on 03-28-2008, 6:37 AM
    • India
    • Posts 209
    Use this in .aspx.cs page......

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl =(DropDownList) e.Row.FindControl("DropDownList1");
                ddl.DataSource = "WhateverUrDataSource";
                ddl.DataTextField = "YourDataTextField";
                ddl.DataTextField = "YourDataValueField";
                ddl.DataBind();
            }
        }

      

    Use this iin .aspx page

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="194px" OnRowDataBound="GridView1_RowDataBound">

                <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
     

     

    "Mark as Answer" on the post that helped you.

    Chandan,
    Imfinity India Pte Ltd.
  • Re: GETTING THE CONTROL ID OF DROPDOWNLIST AND SQLDATASOURCE INSIDE A GRIDVIEW ITEMTEMPLATE

    05-16-2008, 1:13 AM
    • Loading...
    • darkcat02
    • Joined on 05-05-2008, 10:27 PM
    • Posts 45

    chandan.max:
    Use this in .aspx.cs page......

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl =(DropDownList) e.Row.FindControl("DropDownList1");
                ddl.DataSource = "WhateverUrDataSource";
                ddl.DataTextField = "YourDataTextField";
                ddl.DataTextField = "YourDataValueField";
                ddl.DataBind();
            }
        }

      

    Use this iin .aspx page

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="194px" OnRowDataBound="GridView1_RowDataBound">

                <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
     

     

     

     

    how about the selected value... it can't read it... and also.. if i update the grid.. the data wouldn't save...

     

     

     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            Dim drpUnit1 As DropDownList
    
            If Not IsNothing(e.Row.FindControl("drpUnit1")) Then
                drpUnit1 = CType(e.Row.FindControl("drpUnit1"), DropDownList)
                drpUnit1.DataSource = sqlDrpUnit
                drpUnit1.DataTextField = "Unit"
                drpUnit1.DataValueField = "UnitID"
                If Not IsNothing(CType(e.Row.FindControl("drpUnit1"), DropDownList).Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "UnitID").ToString())) Then
    
                    CType(e.Row.FindControl("drpUnit1"), DropDownList).SelectedValue = DataBinder.Eval(e.Row.DataItem, "UnitID").ToString()
    
                End If
                drpUnit1.DataBind()
    
    
    
    
            End If
        End Sub
      
Page 1 of 1 (11 items)