Results 1 to 17 of 17

Thread: link button and button

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    link button and button

    Code:
               //Link button
                LinkButton lnk = new LinkButton();
                lnk.Text = " Click Me";
    
                //button
                Button btn = new Button();
                btn.Text = "Click Me";
    i try put the code above to create new Link button and button but problem now is how can put the code; Response.Redirect? and how can I set the Session["ID"]?

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Hello,

    So, to be clear, it looks like you are asking what how to create controls dynamically, and add them to your page. Is that correct?

    Although this is something that "can" be done, there are a few things that you need to take into consideration, like when the controls should be created, preserving the controls across postbacks, etc. All of these topics are described in this article:

    http://www.4guysfromrolla.com/articles/092904-1.aspx

    And I would encourage you to read it.

    Now, as for your specific question, what you need to do is assign the event handler for the "Click" event of the buttons. Normally, when you create the controls statically, this is done within the ASPX markup for the button, i.e.:

    Code:
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    When you create the control dynamically, you have to do this extra work. This can be done as follows (as a very SIMPLE example):

    Code:
            protected void Page_Load(object sender, EventArgs e)
            {
                Button btn = new Button();
                btn.Text = "Click Me!";
                btn.Click += new EventHandler(btn_Click);
                form1.Controls.Add(btn);
            }
    
            void btn_Click(object sender, EventArgs e)
            {
                Response.Redirect("http://www.bing.com");
            }
    I would also recommend that you take a look at this document:

    http://msdn.microsoft.com/en-us/library/ms743596.aspx

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: link button and button

    what Im doing is creating a link button upon looping of records;

    Code:
            while (dr.Read())
            {
    
                LinkButton lnkButton = new LinkButton();
                lnkButton.Text = "Click";
                lnkButton.Click += new EventHandler(asd_Click);
                cell.Controls.Add(lnkButton);
    
            }
    
        protected void asd_Click(object sender, EventArgs e)
        {
            Session["ID"] = "Demo";
            Response.Redirect("test.aspx");
        }
    There is no bug but the Session["ID"] is still null and test.aspx doesn't show..

    and how can i able to do this code?

    Code:
    Response.Redirect("test.aspx?id=dr["id"].ToString()");

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Hello,

    How, and where, are you checking the Session["ID"] variable. Can you show the code?

    As for the latter part of your question, personally I would do it like this:

    Code:
    Response.Redirect(String.Format("test.aspx?id={0}", dr["id"].ToString()));
    Here I am using String.Format to concatentate two portions of a string together.

    You can read more about this technique here:

    http://msdn.microsoft.com/en-us/libr...ng.format.aspx

    Hope that helps!

    Gary

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: link button and button

    Post your full code.I test this simple code and it worked well for me

    Page 2

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("Session:" + Session["ID"]);
        }
    Page1
    Code:
     protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton btn = new LinkButton();
            btn.Text = "Sample";
            btn.Click += new EventHandler(btn_Click);
            Page.Form.Controls.Add(btn);
        }
    
        void btn_Click(object sender, EventArgs e)
        {
            Session["ID"] = "ID";
            Response.Redirect("Default2.aspx");
        }
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Hello,

    One small thing to point out in Dana's sample...

    Every session variable that you create is actually stored in the collection as a .Net Object. As a result, when you come round to using it again, you should actually cast it correctly to the type of object that it actually is. In the case of a string, that is an implicit cast that works in this case, however, IMO, it is better to handle this yourself, explicitly, in your code.

    Gary

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Also, just one other thing, while I think about it.

    Why are you trying to put the ID on both the QueryString and a Session variable, or are you simply trying both of them out. You really only need to use one, or the other.

    Gary

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: link button and button

    this is my code;

    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
    
                view_prod();
    
        }
    
        private void view_prod()
        {
    
            SqlConnection oConn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
    
            SqlCommand oCmd = new SqlCommand("select * from prods", oConn);
    
            oCmd.CommandTimeout = 300;
            oConn.Open();
    
            SqlDataReader dr = oCmd.ExecuteReader();
    
            while (dr.Read())
            {
    
                TableRow rowItem = new TableRow();
                TableCell cellColumnInfo = new TableCell();
                cellColumnInfo.VerticalAlign = VerticalAlign.Top;
                cellColumnInfo.Width = 600;
    
                Label lblProdName = new Label();
                lblProdName.Font.Bold = true;
                lblProdName.Font.Size = 12;
                lblProdName.Text = dr["ProductName"].ToString();
                cellColumnInfo.Controls.Add(lblProdName);
                rowItem.Cells.Add(cellColumnInfo);
                
    
                //Read More...
                LinkButton lnkButton = new LinkButton();
                lnkButton.Font.Size = 10;
                lnkButton.Text = "  Read More...";
                lnkButton.Click += new EventHandler(btn_Click);
                lnkButton.CommandArgument = dr["id"].ToString();
                cellColumnInfo.Controls.Add(lnkButton);
    
    
                //add the cells to the new row
                myProducts.Rows.Add(rowItem);
    
            }
    
            dr.Close();
            oConn.Close();
            oConn.Dispose();
    
        }
    
        void btn_Click(object sender, EventArgs e)
        {
            Session["ID"] = "ID"; //it should be the btn.ID
            Response.Redirect("Default2.aspx");
        }

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Hello,

    So I take it the above code is for Default.aspx, and you want to redirect to Default2.aspx. Is that correct?

    In which case, have you put the code that Dana has mentioned in the load event of Default2.aspx?

    Gary

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: link button and button

    yup it default2.aspx doesnt show

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Oh, and in order to get the ID of the incoming button, try the following:

    Code:
            void btn_Click(object sender, EventArgs e)
            {
                LinkButton tempButton = (LinkButton)sender;
                Session["ID"] = tempButton.ID;
                Response.Redirect("Default2.aspx");
            }
    Gary

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Quote Originally Posted by arshesander View Post
    yup it default2.aspx doesnt show
    Ok, does that mean you get a HTTP 404 exception, or that something else happens?

    Have you actually created Default2.aspx and placed it at the same location as Default.aspx?

    Gary

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: link button and button

    the page stays at default.aspx... there is not exception message at all

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Set a breakpoint in the btn_Click event. Does the breakpoint get hit?

    Gary

  15. #15
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: link button and button

    I doubt this could be because of this one

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
    
                view_prod();
    
        }
    Try placing the ! Page.IsPostBack as

    Code:
    if ( ! Page.IsPostBack )
            {
                  view_prod();
    
            }

    When you click the Linkbutton the page reloads and controls are rebuilt and then only click event fires
    Please mark you thread resolved using the Thread Tools as shown

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: link button and button

    Set a breakpoint in the btn_Click event. Does the breakpoint get hit?
    btn_Click doesnt work even it has a breakpoint..

    the same thing with
    if ( ! Page.IsPostBack )
    {
    view_prod();

    }

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: link button and button

    Hello,

    If the breakpoint is not being hit, this means that the hook up of the click event handler, i.e:

    Code:
    lnkButton.Click += new EventHandler(btn_Click);
    Is not working.

    Take a step back and try the code that has been presented here, i.e. without the Databinding that you are using and make sure that this is working first.

    Then you can start to add in your other code.

    I suspect that you are going to have to move the creation of the button out of the Page_Load event, and perhaps in the Page_Init event, however, this is going to be difficult given that the creation of the button is based on the information that you are pulling back from the database.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width