Change in value of custom attribute is not reflecting at server side

T

The Developer

Hi All,
I have a web application where I am adding a custom attribute to my ASP.NET
text box control and changing value of that attribute at client side using
JavaScript. My problem is that changed value of that custom attribute is not
reflecting back at server side. Any ideas about this problem?

Server side code:
private void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
this.txtTarget.Attributes.Add("myCustomAttribute", "Server Side Value");
this.txtTarget.Text = "Server Side Value";
}
else
{
Response.Write("Attribute Value In Page_Load: <b>"
+ this.txtTarget.Attributes["myCustomAttribute"]
+ "</b><br>");
}
}

Client side code:
<script>
function setTextBoxAttribute()
{
var txtTarget = document.getElementById('txtTarget');

alert('Old Attribute Value: ' +
txtTarget.getAttribute('myCustomAttribute'));

txtTarget.setAttribute('myCustomAttribute', 'Client Side Value');
txtTarget.value = 'Client Side Value';
}
</script>

PS: Using input hidden HTML control is not an option for me. I have to use
custom attributes anyhow.
 
K

Karl Seguin

Developer:
This simply isn't something that asp.net automatically takes care of. It's
actually quite impossible for ASP.Net to automatically do this. You can
certainly take a look at:
http://www.openmymind.net/FAQ.aspx?documentId=1 which talks about why
dynamically added options to a select don't show up in postback...and you
can apply the same logic to your case.

When a page is posted back, individual attributes aren't posted back...this
is just how HTTP works...ergo ASP.Net can't track changes. I find it
curious how hidden fields aren't an option, since you are relying on
viewstate (which is a hidden field) to do this for you...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
T

The Developer

Thanks for your reply.

In my real application, I'll be having several custom attributes for every
template column in my datagrid. On using hidden field for every of them will
result in increase in size of resulting HTML and in my opinion, using custom
attributes will be the most optimum solution.

I saw an article on codeproject sometime back which uses similar concept in
his VB.NET web application but I am not able to find that article now. So, I
can surely say that this type of behavior is not "impossible" for ASP.NET.
:)

Any help will be appreciated for this problem.

Karl Seguin said:
Developer:
This simply isn't something that asp.net automatically takes care of. It's
actually quite impossible for ASP.Net to automatically do this. You can
certainly take a look at:
http://www.openmymind.net/FAQ.aspx?documentId=1 which talks about why
dynamically added options to a select don't show up in postback...and you
can apply the same logic to your case.

When a page is posted back, individual attributes aren't posted back...this
is just how HTTP works...ergo ASP.Net can't track changes. I find it
curious how hidden fields aren't an option, since you are relying on
viewstate (which is a hidden field) to do this for you...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
The Developer said:
Hi All,
I have a web application where I am adding a custom attribute to my ASP.NET
text box control and changing value of that attribute at client side using
JavaScript. My problem is that changed value of that custom attribute is not
reflecting back at server side. Any ideas about this problem?

Server side code:
private void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
this.txtTarget.Attributes.Add("myCustomAttribute", "Server Side Value");
this.txtTarget.Text = "Server Side Value";
}
else
{
Response.Write("Attribute Value In Page_Load: <b>"
+ this.txtTarget.Attributes["myCustomAttribute"]
+ "</b><br>");
}
}

Client side code:
<script>
function setTextBoxAttribute()
{
var txtTarget = document.getElementById('txtTarget');

alert('Old Attribute Value: ' +
txtTarget.getAttribute('myCustomAttribute'));

txtTarget.setAttribute('myCustomAttribute', 'Client Side Value');
txtTarget.value = 'Client Side Value';
}
</script>

PS: Using input hidden HTML control is not an option for me. I have to use
custom attributes anyhow.
 
B

Bruce Barker

you approach will not work.

when the browser does a form submit (postback in .net terms), it posts the
control name and value (name=value) only. it will only postback <input>,
<button>, <select> and <textarea> elements inside the posting form. they
must also be enabled and have a name.


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top