locked
ViewState RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code and i want when user clicks Add , txtCode field should have ViewState Value if it is not Blank

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myAddModal">Add New</button><br /><br />

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {

    ViewState["Code"] = "";
    }
    }

    Thanks

    Friday, August 17, 2018 7:09 AM

Answers

  • User-893317190 posted

    Hi JagjitSingh,

    You should add onserverclick and runat="server" attribute to your button  so that it can trigger server side event.

           <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myAddModal"  runat="server" onserverclick="Button1_Click">Add New</button><br /><br />

    As to how to deal with it at server side, I have posted to you my code. If you have any problem with my code , please tell me more specifically. I don't know why you ask this question after I have posted you my code.

    protected void Button1_Click(object sender, EventArgs e)
            {
                
               
                if (!string.IsNullOrEmpty(txtCode.Text ))
                {
                    ViewState["Code"] = txtCode.Text;
                  
                   
                }
                if (!string.IsNullOrEmpty(ViewState["Code"] as string))
                {
                    txtCode.Text = ViewState["Code"] as string;
                }
               
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 20, 2018 8:15 AM

All replies

  • User-893317190 posted

    Hi  JagjitSingh,

    You could use the method  string.IsNullOrEmpty in the button’s  serverclick event.
    Below is my code.

      <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
    
           <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myAddModal"  runat="server" onserverclick="Button1_Click">Add New</button><br /><br />
    

    Code behind.

      protected void Button1_Click(object sender, EventArgs e)
            {
                if(string.IsNullOrEmpty(ViewState["Code"] as string))
                {
                    ViewState["Code"] = txtCode.Text;!
                   
                }
                // test the effect
                Response.Write(ViewState["Code"] as string);
            }
    

    Because I’m not very clear what “it” refers to in your expression if it is not blank, if you refer to the txtCode field, you could write your code as follows.

    protected void Button1_Click(object sender, EventArgs e)
            {
                if(!string.IsNullOrEmpty(txtCode.Text ))
                {
                    ViewState["Code"] = txtCode.Text;
                   
                }
                // test the effect
                Response.Write(ViewState["Code"] as string);
            }
    

    Best regards ,

    Ackerly Xu

    Monday, August 20, 2018 2:08 AM
  • User-1499457942 posted

    Hi

      I want the opposite . I want when click on Add value in Viewstate should get displayed in Modal Popup textbox txt_Code

    Thanks

    Monday, August 20, 2018 4:44 AM
  • User-893317190 posted

    Hi  JagjitSingh,

    You could try code below. 

    protected void Button1_Click(object sender, EventArgs e)
            {
                
               
                if (!string.IsNullOrEmpty(txtCode.Text ))
                {
                    ViewState["Code"] = txtCode.Text;
                  
                   
                }
                if (!string.IsNullOrEmpty(ViewState["Code"] as string))
                {
                    txtCode.Text = ViewState["Code"] as string;
                }
               
            }
    

    Best regards ,

    Ackerly Xu

    Monday, August 20, 2018 5:22 AM
  • User-1499457942 posted

    Hi Xu

      I have below code on which i want to update txt_code value from ViewState . What should be the code on Server Side

    <button type="button" id="btnAdd" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add New</button><br /><br />

    Thanks

    Monday, August 20, 2018 5:38 AM
  • User-893317190 posted

    Hi JagjitSingh,

    You should add onserverclick and runat="server" attribute to your button  so that it can trigger server side event.

           <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myAddModal"  runat="server" onserverclick="Button1_Click">Add New</button><br /><br />

    As to how to deal with it at server side, I have posted to you my code. If you have any problem with my code , please tell me more specifically. I don't know why you ask this question after I have posted you my code.

    protected void Button1_Click(object sender, EventArgs e)
            {
                
               
                if (!string.IsNullOrEmpty(txtCode.Text ))
                {
                    ViewState["Code"] = txtCode.Text;
                  
                   
                }
                if (!string.IsNullOrEmpty(ViewState["Code"] as string))
                {
                    txtCode.Text = ViewState["Code"] as string;
                }
               
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 20, 2018 8:15 AM