Click here to Skip to main content
15,851,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to access dynamic controls id in code behind in my aspx page but i didn't get it how to get my dynamic id.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:repeater ID="myRepeater" runat="server"
                  <asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" UseSubmitBehavior="false" runat="server" Text="Button" CommandName="myfunction" />


and my code behind code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
    public static string Connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True";
    public string code;
    public string id
        { return id; }
        {  id=value; }

    protected void Page_Load(object sender, EventArgs e)
        System.Data.SqlClient.SqlConnection con = new SqlConnection(Connection);
        System.Data.SqlClient.SqlCommand cmd = new SqlCommand("select * from COMPLAINS", con);
        System.Data.SqlClient.SqlDataAdapter dp = new SqlDataAdapter(cmd);
        System.Data.DataTable dtu = new DataTable();
        myRepeater.DataSource = dtu;
    protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        var textbox = e.Item.FindControl("myTextBox");
        textbox.ClientIDMode = ClientIDMode.Static;
        textbox.ID = "myTextBox" + (e.Item.ItemIndex + 1);
    protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        if (e.CommandName == "myfunction")
            string textbox = e.Item.ItemIndex.ToString();
            textbox = "myTextBox" + textbox;
            var nme = e.Item.FindControl(textbox);

i have create my textbox id's like myTextBox1,myTextBox2,myTextBox3 and so on itemdatabound . but now i want to access my textbox id's on itemcommand but i can't access my id in code says null value when this method
if (e.CommandName == "myfunction")
            string textbox = e.Item.ItemIndex.ToString();
            textbox = "myTextBox" + textbox;
            var nme = e.Item.FindControl(textbox);


please help me
Updated 28-Oct-15 2:59am

1 solution

When you assign the IDs you assign ItemIndex+1, but when you access the control in your command event you use ItemIndex. So your first box is myTextBox1 but your code looks for myTextBox0.

protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    if (e.CommandName == "myfunction")
        // add the "+ 1" here too
        string textbox = (e.Item.ItemIndex + 1).ToString();
Share this answer
Mohammad Nawaz 28-Oct-15 9:17am    
no sir whenever i tried to click mytextbox2 then that time also show only null value
F-ES Sitecore 28-Oct-15 9:21am    
Well the updated code worked for me.
Mohammad Nawaz 28-Oct-15 14:07pm    
can i have ur code its not working sir
F-ES Sitecore 29-Oct-15 5:06am    
I posted the amendment I made in my solution, you need to add "+1" in your ItemCommand event handler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900