2

I have two buttons with on click functions

The 1st one gets assigned a variable when Clicked.

How do I get my second button to get the variable from the 1st button when I click button 2?

It doesn't seem to work. As the second button doesn't recognise the Variable.

Thanks

EDIT:

Just to clarify My code is generating a pdf. button 1 selects the url of the template to use. and in button 2 (the one generating the pdf) I want it to get the variable set from button 1 so it knows what template to use.

EDIT 2: My code does work but only when I'm not using the ajax update panel. it seems that the variable I'm trying to set doesn't get set with AJAX

7
  • 1
    Can you share your code with what you have tried? Commented Sep 12, 2012 at 15:29
  • There is nothing to share, imagine two barebone buttons with on click functions. The 1st gets assigned a string. and I want the second button to be able to use that string Commented Sep 12, 2012 at 15:30
  • 1
    You need to understand "Scope" - here's a link: msdn.microsoft.com/en-us/library/ms973875.aspx Commented Sep 12, 2012 at 15:30
  • Thanks Ron I will have a look Commented Sep 12, 2012 at 15:31
  • Well, I typed up a comment as to why the variable doesn't persist, but it appears the person who wrote the answer I commented on has deleted their answer, taking my comment with it. Commented Sep 12, 2012 at 15:48

2 Answers 2

2

Your Button have Id, you get this button with his Id

Nota : You can add runat="server" in order to visualize in server side

<asp:Button id="Button1"
           Text="Click "
           OnClick="Btn1_Click" 
           runat="server"/>

<asp:Button id="Button2"
           Text="Click "
           OnClick="Btn2_Click" 
           runat="server"/>

void Btn2_Click(Object sender, EventArgs e)
{
    Button1.Text = "test after click on button 2";

    Template = ...;//Set your value
}


void Btn1_Click(Object sender, EventArgs e)
{
    Button2.Text = "test after click on button 1";

    //Here you can get your value after post.
    var result = Template; 
}

It's not subject but in delegate you can also get objet button by passing on sender argument.

var button = sender as Button; //You get button who raise event

In order to manage Template Path property.

public string Template
{
   get
   {
      if(ViewState["Template"] != null)
      {       
         return (string)ViewState["Template"];
      }
   }

   set{ViewState["Template"] = value;} 
}
Sign up to request clarification or add additional context in comments.

5 Comments

This works because Button1.Text and Button2.Text have a persistence mechanism - i.e. the Button control uses the concept of the ASP.NET ViewState. Which is what you need to understand to persist state.
Exactly TomFanning and EnableViewState="true" by default, thank's for additional remark
I have view state turned off! that could be my problem as I tried this before asking the question. I will give this a go.
I tried this and it's not working. im not trying to change any properties. My code is generating a pdf. button 1 selects the url of the template to use. and in button 2 (the one generating the pdf) I want it to get the variable set from button 1 so it knows what template to use
You add template Path in ViewState, just this VOX, i update my answer
-1

i guess you are looking at accessing value of a variable inside the click event of button2 for which the value is set in the button1 click event ?

private string myPrivateString = "";

void Page_Load()//Not sure of correct method signature
{
  if(Page.IsPostBack)
  {
    myPrivateString = Session["myPrivateString"];
  }
}

void Button1_Click(object sender, EventArgs e)
{
  //There will a postback before this gets executed
  myPrivateString = "Value Set From Button 1";
  Session["myPrivateString"] = myPrivateString;
}

void Button2_Click(object sender, EventArgs e)
{
  //There will a postback before this gets executed
  //Accessing myPrivateString here without setting value from session
  //will return empty string as after PostBack its a new page thats rendered.
  myPrivateString = Session["myPrivateString"]; // Or do it in the Page_Load event
}

I guess now you can get the value of inside the button2 click event.
Also read about ASP.NET Page lifecycle and how client side events like button clicks are handled by the ASP.NET framework.

5 Comments

In this context you should really be using view state, not the session.
@Servy he didn't ask for control properties, he wanted value of a global variable to be accessed inside two different events ? ViewState is only for Control properties like Text, etc. Why the downvote ?? Look at the tags.
View State isn't just for control properties. Control properties are stored there, but so are a number of other things, including appropriate user content. It's all a matter of scope. ViewState content is scoped to this page, and all of the postbacks to this page. It is passed back and forth with each request/response as hidden HTML fields. Session is stored in memory on the server, and will live well beyond the scope of this single page's lifecycle. That's bad, both because it's wasteful of resources, and because it means you can't ever have this page open twice (without bugs anyway).
what do you mean by opening twice ? if by the same user in that case session state is better as it will recognize the user and associated data within the session duration.
If you have the same window open in two tabs they will be sharing the same session variable, but in this context they really shouldn't be. Each window (or tab) should have it's own variable. Pushing a button on one window (in this context) shouldn't be affecting another window.

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.