Click here to Skip to main content
11,481,404 members (58,001 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hello Friends,
I want to fetch Itemname on Dynamically generated LinkButtons...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
public partial class Dynamic_link : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    SqlConnection cnn = new SqlConnection();
    void page_int(object sender, EventArgs e)
    {
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
        string sql = "select itemname from menu where activestatus=1";
        SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
        DataTable dt = new DataTable();
            string val = dt.Rows[0]["itemname"].ToString();
        dt.Columns.Add("itemname", typeof(string));
        dt.Rows.Add("itemname");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
             for (int j = 0; j < dt.Columns.Count; j++)
            {
                LinkButton lb = new LinkButton();
                lb.ID = "link_Click";
                lb.Text = val;
                lb.Click += new System.EventHandler(link_Click);
                this.Controls.Add(lb);
            }
       }
       
    }
    protected void link_Click(object sender, EventArgs e)
    {
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Click me...", script type = 'text/javascript'>alert('LinkButton Clicked')/script>");
    }
}

Can Anyone, help me...to get the solution...
Actually, i have inserted, menu table trough stored proceedure...
the, stored procedure is this:

ALTER PROCEDURE [dbo].[str_collection]
@itemname nvarchar(50),
@item_img nvarchar(MAX),
@subitem_name nvarchar(50),
@item_rate decimal(18,2),
@item_descript nvarchar(100)
AS
BEGIN
SET NOCOUNT ON
declare @id nvarchar(50) 
SET @id=LEN(@subitem_name)
declare @item_id nvarchar(50)
declare @entrydate as date
SET @entrydate= GETDATE()
SET @item_id=@itemname+@id
INSERT INTO menu(itemname,item_id,item_img,entrydate)values(@itemname,@item_id,@item_img,@entrydate)
INSERT INTO submenu(item_id,item_descript,item_rate,subitem_name)values(@item_id,@item_descript,@item_rate,@subitem_name) 
END



Hello friends,
I m using, Repeater....now, and code which i have done is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
 
public partial class Dynamic_link : System.Web.UI.Page
{
    SqlConnection cnn = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        cnn.ConnectionString=ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
        if(cnn.State==ConnectionState.Closed)
        {
            cnn.Open();
        }  
        SqlCommand cm = new SqlCommand("select itemname from menu", cnn);  
          cm.CommandType = CommandType.Text;  
         SqlDataAdapter da = new SqlDataAdapter(cm);
        DataSet dt = new DataSet();
        da.Fill(dt);
         Repeater Repeater1 = new Repeater();
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }  
        foreach (RepeaterItem repeatItem in Repeater1.Items)
        {
            // if condition to add HeaderTemplate Dynamically only Once  
            if (repeatItem.ItemIndex == 0)
            {
                RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
                HtmlGenericControl hTag = new HtmlGenericControl("h4");
               hTag.InnerHtml = "Menu Collection";
                repeatItem.Controls.Add(hTag);
            }
 
            // Add ItemTemplate DataItems Dynamically  
            RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
            LinkButton lnk = new LinkButton();
            lnk.ID = "lnk_itemname";
            lnk.Text = String.Format("{0}{1}<br/>", dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]);
            repeatItem.Controls.Add(lnk);
           // Add SeparatorTemplate Dynamically  
            repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
            LiteralControl ltrlHR = new LiteralControl();
            ltrlHR.Text = "<hr />";
            repeatItem.Controls.Add(ltrlHR);
        }
 
        // Add Repeater Control as Child Control  
        // of Panel Control  
        Panel1.Controls.Add(Repeater1);  
    }
    
}

So I m Getting Error , on this Line:
lnk.Text = String.Format("{0}{1}<br />", dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]);

and the error is:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Posted 5-Mar-13 4:22am
v5
Comments
ThePhantomUpvoter at 5-Mar-13 9:26am
   
Okay..... so query your database in your link_Click handler....
Ankit_Sharma1987 at 5-Mar-13 9:35am
   
Thankx sir...
Please expain me...little bit....
You mean to say, i have to have to change here?
lb.Click += new System.EventHandler(link_Click);
ThePhantomUpvoter at 5-Mar-13 9:54am
   
No I did not say that at all. If you want to query your database, then in your link_Click event handler, query the database. It's a bit hard to figure out exactly what it is that you are asking when you have already written the code that does what you want.... You have a handler for the click so why are you not querying the database there just like you say you want to?
Ankit_Sharma1987 at 5-Mar-13 10:04am
   
Sir You mean to say , like this?

protected void link_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Click me...", "<script type = 'text/javascript'>alert('LinkButton Clicked')</script>");
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
string sql = "select itemname from menu where activestatus=1";
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
DataTable dt = new DataTable();
dt.Columns.Add("itemname", typeof(string));
dt.Rows.Add("itemname");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string val = dt.Rows[0]["itemname"].ToString();
}
}
}
ryanb31 at 5-Mar-13 9:42am
   
In link_click do whatever code you need. I guess we are confused as to what you are missing.
Ankit_Sharma1987 at 6-Mar-13 3:02am
   
Hello friends,
I m using, Repeater....now, and code which i have done is here:

<pre lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Dynamic_link : System.Web.UI.Page
{
SqlConnection cnn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
cnn.ConnectionString=ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
if(cnn.State==ConnectionState.Closed)
{
cnn.Open();
}
SqlCommand cm = new SqlCommand("select itemname from menu", cnn);
cm.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cm);
DataSet dt = new DataSet();
da.Fill(dt);
Repeater Repeater1 = new Repeater();
Repeater1.DataSource = dt;
Repeater1.DataBind();
if (cnn.State == ConnectionState.Open)
{
cnn.Close();
}
foreach (RepeaterItem repeatItem in Repeater1.Items)
{
// if condition to add HeaderTemplate Dynamically only Once
if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
HtmlGenericControl hTag = new HtmlGenericControl("h4");
hTag.InnerHtml = "Menu Collection";
repeatItem.Controls.Add(hTag);
}

// Add ItemTemplate DataItems Dynamically
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
LinkButton lnk = new LinkButton();
lnk.ID = "lnk_itemname";
lnk.Text = String.Format("{0}{1}<br/>", dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]);
repeatItem.Controls.Add(lnk);
// Add SeparatorTemplate Dynamically
repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "
";
repeatItem.Controls.Add(ltrlHR);
}

// Add Repeater Control as Child Control
// of Panel Control
Panel1.Controls.Add(Repeater1);
}

}
</pre>

So I m Getting Error , on this Line:
lnk.Text = String.Format("{0}{1}<br/>", dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]);

and the error is:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yes ,I solved It...
Now The, Linkbuttons are generated Dynamically, and..linkbutton text is Fetched From Database..
The Code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
 
public partial class Dynamic_link : System.Web.UI.Page
{
    SqlConnection cnn = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        cnn.ConnectionString=ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
        if(cnn.State==ConnectionState.Closed)
        {
            cnn.Open();
        }
        SqlCommand cm = new SqlCommand("select itemname from menu", cnn);
          cm.CommandType = CommandType.Text;
         SqlDataAdapter da = new SqlDataAdapter(cm);
        DataSet dt = new DataSet();
        da.Fill(dt);
         Repeater Repeater1 = new Repeater();
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }
        foreach (RepeaterItem repeatItem in Repeater1.Items)
        {
            // if condition to add HeaderTemplate Dynamically only Once
            if (repeatItem.ItemIndex == 0)
            {
                RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
                HtmlGenericControl hTag = new HtmlGenericControl("h4");
               hTag.InnerHtml = "Menu Collection";
                repeatItem.Controls.Add(hTag);
            }
 
            // Add ItemTemplate DataItems Dynamically
            //SqlDataReader dr = Int32.Parse;
            RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
            LinkButton lnk = new LinkButton();
            lnk.ID = "lnk_itemname";
            lnk.Text = String.Format("{0}",(dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]));
            repeatItem.Controls.Add(lnk);
           // Add SeparatorTemplate Dynamically
            repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
            LiteralControl ltrlHR = new LiteralControl();
            ltrlHR.Text = "<hr />";
            repeatItem.Controls.Add(ltrlHR);
        }
 
        // Add Repeater Control as Child Control
        // of Panel Control
        Panel1.Controls.Add(Repeater1);
    }
 
}

And Design Source is:

asp:Panel ID="Panel1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <h4>
            Menu Items </h4>
    </HeaderTemplate>
    <ItemTemplate>
        <%#DataBinder.Eval(Container.DataItem,"itemname") %>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater>
</asp:Panel>
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100