Click here to Skip to main content
15,902,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one button on one page. I want to access the text value of that button on the next page where I am redirecting.To access values of text boxes I have written following code
Request.Form["txtnme"].tostring();
What should I write for button?
Posted

I have created a sample ASP.NET Web Form application. Followings are the steps I have taken:

1. In Default.aspx, modify the "MainContent" placehoder like following:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:Button ID="btnSample" runat="server" Text="Button Text Value"  PostBackUrl="~/About.aspx" />
    <asp:TextBox ID="TxtbtnSample" ClientIDMode="Static" runat="server" Text="Some Text Value"></asp:TextBox>
</asp:Content>



2. You need to focus on correct key for the control generated by application. Use ViewSource of the Default.aspx page(or your previous page) by right clicking on the page in the browser. You will be able to see something like:

XML
<section class="content-wrapper main-content clear-fix">

   <input type="submit" name="ctl00$MainContent$btnSample" value="Button Text Value" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSample&quot;, &quot;&quot;, false, &quot;&quot;, &quot;About.aspx&quot;, false, false))" id="MainContent_btnSample" />
   <input name="ctl00$MainContent$TxtbtnSample" type="text" value="Some Text Value" id="TxtbtnSample" />

       </section>


Just take the value of Name property of the control (input or button tags) and use in your code behind of your next page.

3. Now in About.aspx.cs, I wrote like:

C#
protected void Page_Load(object sender, EventArgs e)
       {
           string txtSting = Request.Form["ctl00$MainContent$TxtbtnSample"].ToString();
           string btnSting = Request.Form["ctl00$MainContent$btnSample"].ToString();
       }



And I am able to get value of Textbox as well as Button Text.


Hope it will help you. Thanks.
 
Share this answer
 
v5
I've used SESSION for passing the text of button to other page.

--Button.aspx--(main page)
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn1" Text="Submit" runat="server" onclick="btn1_Click" />
    <asp:Label ID="lbl1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


--Button.aspx.cs---

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class button : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string abc = "";
        abc = btn1.Text;
        Session["button"] = abc;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}



---Default.aspx---- (redirected page)

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
<div>
    <form id="form1" runat="server">

    <asp:Label ID= "lbl2" runat="server"></asp:Label>
     </div>
    </form>
</body>
</html>


----------Default.aspx.cs------

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        lbl2.Text = Session["button"].ToString();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {


    }
}
 
Share this answer
 
Using QueryString for passing the text of button to other page.

--Button.aspx.cs---

<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class button : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string abc = &quot;&quot;;
abc = btn1.Text;
Session[&quot;button&quot;] = abc;
}
protected void btn1_Click(object sender, EventArgs e)
{
Response.Redirect(
"Default.aspx
?button="+btn1.Text);
}
}


----------Default.aspx.cs------

<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

lbl2.Text =Request.QueryString["button"].ToString();
}
protected void btnsubmit_Click(object sender, EventArgs e)
{


}
}</pre>
 
Share this answer
 

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