0

I have a hidden variable in my aspx page as below.

  <input type="hidden" value="" runat="server" id="homeExcelData" filename=""/>

On click of a button, iam doing a a form.Sumbit();

Before submitting, i am setting the properties of the hidden control as below.

document.getElementById("homeExcelData").attributes["filename"] = "test.xls";
                            $("#homeExcelData").attr("filename","test.xls");
                            $("#homeExcelData").prop("filename","test.xls");
                            alert($("#homeExcelData").attr("filename"));
                            $("#homeExcelData").val(excelData);

In the code behind, during postback, i can get the excelData by homeExcelData.Value which i set in the javascript.

But the value of the attribute "filename" is coming as empty string(not as null) instead of "test.xls".

Please help me to fix it.

Server side code:

 string fileName = homeExcelData.Attributes["filename"] ?? "report.xls";
1
  • Values will be lost when postpack. So you should keep values in ViewState. Commented Mar 12, 2012 at 9:49

2 Answers 2

2

Why do you think the filename attribute value should be part of the POST request to the server ?

( It will only be part of the viewstate value if you set it server-side )

I would have another hidden field "homeExcelDataFileName" and use its value.

Hope this will help,

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

3 Comments

Thats right. But when the form.Submit() is done, the view state is refreshed & reconstructed in the aspx page and submitted back to server. If your adding a value to the control via js, it comes to the server as control.Value. Which means that for certain HTML attributes, the viewstate is reconstructed fine. But for custom HTML attributes, the viewstate is not refreshed. So basically i need a some trick to make asp.net read the custom attributes and pack it into the viewstate of the control. then it would come automatically to server.
Its not just file name, i need to add a few more custom attributes. May be i need to recontruct my value as a complex json wit these attributes.
The viewstate should never be modified or reconstructed client-side. As far as I know, submit() does not alter the view state before issuing the post request. ViewStateMac is here to help check that ("Invalid ViewState"). You may use some js to gather, encode and "pack" your data in some hidden field which value will be decrypted server side to alter your controls features.
1

The above works for me. I think what is happening is that on postback of your submit button the value is somehow getting re initialized (Your javascript for adding attributes is loading twice-moving it to server side works for me)

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set attribute on page Load
        homeExcelData.Attributes.Add("filename","test.xls");
    }
}

protected void btnCheckValues_Click(object sender, EventArgs e)
{
    Response.Write( homeExcelData.Attributes["filename"].ToString());

}

2 Comments

If we are adding it in code-behind, then it works fine. the problem occurs if we add it in javascript and did a from.Sumbit() instead of button_click
I think adding an attibute & value client side might not get rendered on page so it is turning out empty.

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.