Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir/Madam,
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.



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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:repeater ID="myRepeater" runat="server"
            onitemdatabound="myRepeater_ItemDataBound"
            onitemcommand="myRepeater_ItemCommand">
            <ItemTemplate>
                  <asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" UseSubmitBehavior="false" runat="server" Text="Button" CommandName="myfunction" />
            </ItemTemplate>

            </asp:repeater>
    </div>
    </form>
</body>
</html>



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
    {
        get
        { return id; }
        set
        {  id=value; }
    

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection con = new SqlConnection(Connection);
        con.Open();
        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();
        dp.Fill(dtu);
        myRepeater.DataSource = dtu;
        myRepeater.DataBind();
        
    }
    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 behind.it says null value when this method
C#
if (e.CommandName == "myfunction")
        {
            string textbox = e.Item.ItemIndex.ToString();
            textbox = "myTextBox" + textbox;
            var nme = e.Item.FindControl(textbox);

        }

please help me
Posted
Updated 28-Oct-15 1:59am
v2

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.

C#
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
 
Comments
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