0

I'm trying to call a function created in code through <asp:Button>'s on click event but its not working...

protected void Page_Load(object sender, EventArgs e)
{       
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
            ArrayList myArrayList = ConvertDataSetToArrayList();
            Literal objliteral = new Literal();
            StringBuilder objSBuilder = new StringBuilder();

        // Display each item of ArrayList
        foreach (Object row in myArrayList)
        {
            objSBuilder.Append("<div class='AppItem'>");                
            objSBuilder.Append("<label class='control-label span3'>" + ((DataRow)row)["PlanName"].ToString() + "</label>");
            objSBuilder.Append("<label class='control-label span3'> Plan Description :" + ((DataRow)row)["PlanDesc"].ToString() + "</label>");
            objSBuilder.Append("<label class='control-label span3'> Price :" + ((DataRow)row)["PlanCost"].ToString() + "</label>");
            objSBuilder.Append("<label class='control-label span3'> Total Visitors :" + ((DataRow)row)["PlanVisitors"].ToString() + "</label>");
            objSBuilder.Append("<div class='control-group'><asp:Button Text='AddtoCart' runat='server' id='" + ((DataRow)row)["PlanId"].ToString() + "' class='btn btn-small btn-success' onclick='AddPlanToCart();' /></div>");

            //<asp:Button ID="ImageButton1" runat="server" Text="Upload" CssClass="btn btn-small btn-success" onclick="ImageButton1_Click" />
            objSBuilder.Append("</div>");
        }
        objliteral.Text = objSBuilder.ToString();
        PlanContent.Controls.Add(objliteral);
}

private void AddPlanToCart()
{    
        //This does not get called.
}

Click here to see code behind!

3
  • 7
    Please place your code snippets for the question on SO -- no one wants to click links. Plus, it doesn't help anyone in the future with the same issues. Commented Jul 18, 2013 at 13:32
  • ImageButton1_Click this event? the one for the button that's commented out? Where is the event function? I don't see it in the code. Commented Jul 18, 2013 at 13:34
  • @David Scott : IamgeButton is commented code. Commented Jul 18, 2013 at 13:58

3 Answers 3

0

The problem here is that you're generating the code to run on the client-side, but including scripts and tags designed to be run through the asp.net processor:

objSBuilder.Append("<div class='control-group'><asp:Button Text='AddtoCart' runat='server' id='" + ((DataRow)row)["PlanId"].ToString() + "' class='btn btn-small btn-success' onclick='AddPlanToCart();' /></div>");

which produces HTML including [slightly reformatted]:

<div class='control-group'>
    <asp:Button ... onclick='AddPlanToCart();' />
</div>

instead of the <button> control that you're obviously expecting.

Fixing this to work in the manner its written in would be a significant amount of work. Instead, I'd strongly recommend rewriting it to use an <asp:repeater> control instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Your onclick is attempting to call a javascript function AddPlanToCart(). You have a couple of options. Here's 2:

1) Create a javascript that does a postback with a specific query string. Check for the query string in page_load, if it exists then call the function.

2) Create an actual dynamic control. It should be created in Page_init if you want to track it's viewstate, not Page_load. Then Add an event handler for the button.

Button b2 = new Button();
b2.Text = "Add To Cart";
b2.Click += new EventHandler(AddPlanToCart);

5 Comments

i hope you got my point what i'm actually doing is creating an table by getting data from database and i'm setting up button's id with PlanId from database and there i want to call a function in which i want to get all records from database where PlanId = this.id this means which one button is clicked.
i'm new to asp.net c# can you please edit my code and send it to me again ?
@ArslanAhmad Yes I know your point. For the javascript suggestion, you can pass the planId as the query string. And you can just do all that with the dynamic controls. Don't create markup language for the controls. Actually create the controls - stackoverflow.com/questions/13106967/…
@ArslanAhmad No. I'm here to help you learn. Not to do your work for you. Start googling and trying a few of the suggested options.
Thanks bro .. but if i wouldn't find any solution i'll come back to you ok ?
0

When creating the button, you can also bind the click event, to a delegate like the following:

Button btn = new Button();
btn.Text = "Button text";
btn.AutoSize = true;
btn.Location = new Point(y, 0);
btn.Click += delegate(object sender, EventArgs e)
{
    solution.Invoke(); 
}; 

In this case I am passing an Action<>, as a parameter within the method. When you click on the button, the delegate would be triggered. For instance, this button would be created, and when clicked, a messagebox would show:

Button btn = new Button();
btn.Text = "Button text";
btn.AutoSize = true;
btn.Location = new Point(y, 0);
btn.Click += delegate(object sender, EventArgs e)
{
    MessageBox.Show("Clicked!");
}; 

For your scenario, try something within these lines:

int counter = 0;

// Display each item of ArrayList
foreach (Object row in myArrayList)
{
    Control ctrl = new Control();

    Label lbl = new Label();
    lbl.Text = "Label " + counter.ToString();

    Button btn = new Button();
    btn.Text = "Button text " + counter.ToString(); 
    btn.Click += delegate(object sender, EventArgs e)
    {
        Response.Write("<script>alert('Message for button " + counter.ToString() + "');</script>");
    }; 

    ctrl.Controls.Add(lbl);
    ctrl.Controls.Add(btn);

    counter++; 

    PlanContent.Controls.Add(ctrl);
}

3 Comments

i'm new to asp.net c# can you please edit my code and send it to me again ? plz
I updated my answer. I am creating a control on each iteration, adding a label, and a button each time. When ready I am adding this control, to PlanContent. Each button opens up a message box with JavaScript.
can you come to my machine ? via Teamviewer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.