3

I want to share the currentTab variable which exists on the C# server side with JavaScript. Here is my code:

C#:

public int currentTab = 1;

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "showTab(" + currentTab + ");", true);
}

JavaScript:

var currentTab = "<%=currentTab%>";

function showTab(index)
{
    currentTab = index;
    // Show tab at (index)
}

I used this approach to get the current tab again on PostBack. However, currentTab on C# is remains 1 after PostBack. How can I solve this issue?

3 Answers 3

2

You can write the index from your javascript function, to a hiddenfield, and then read that on postback.

In your code, you check the hiddenfield, if your page is postback. Like so:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            currentTab = Int32.Parse(HiddenTabValue.Value);
        }

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

1 Comment

In your Page_Load, you can check if IsPostback is true. If it is true, do your postback logic. Like parsing the value of the hiddenfield, into an int.
2

Have a server side hidden field to hold this piece of information.

You can access the field through javascript and as a server side control the value will be available server side.

Comments

1

You need to use some server control to send value back to the server (i.e. asp:HiddenField) or use query string to set the tab index there.

Comments

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.