Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabled control values

Status
Not open for further replies.

kenrevak

Programmer
Mar 13, 2007
9
0
0
CA
New to ASP.NET.

If a control is disabled, readonly, invisible the value it contains is not returned to the server. How do you handle detecting when the data is not returned as opposed to when it is just blank? (Created a default value and detecting that sounds a little archaic but I can't think of anything else!)
 
Can you explain what it is you want to do? It's unclear what you are asking.
 
Simple scenario. You have and textbox on the screen. The user is not authorized to update it so you set ReadOnly=true. This causes ASP.Net to not return the contents of the field on the postback. How do you detect that the text is blank because of the readonly as opposed to the contents actually being blank. It looks like the readonly and enabled states do not make the round trip either.
 
Works fine in my simple test:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="Testing" ReadOnly="true"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
Code:
Partial Class Default12
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(TextBox1.Text)
    End Sub
End Class


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
If you really need to know the difference.. then do
If textbox1.ReadOnly = TRUE AND Textbox1.Text = String.Empty then ....
 
And you get "Testing" back as the value since the page was recreated with that value on the postback. If you change the value in code (say to "xxx")before sending the page (I'm doing mine in the C# Render method) your code will still be printing "Testing" not the "xxx" that was sent.

The point is that if you set readonly=true the text value does not make it into viewstate. Seems I will have to use the enabled property but I just hate the greying out.
 
Code:
Partial Class Default1
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(TextBox1.Text)
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        TextBox1.Text = "Changed"
    End Sub
End Class
Mine prints "Changed" if I change it in the PreRender event. If this isn't what you are doing, It'll be easier if you provide an example rather than us guessing.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "
<html xmlns="
<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:TextBox ID="TextBox1" runat="server" Text="Original text" ReadOnly="True" ></asp:TextBox>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

</div>

</form>

</body>

</html>



using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{



}

protected override void Render(HtmlTextWriter writer)

{

TextBox1.Text = "new text";

base.Render(writer);

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.Write("TestBox1=" + TextBox1.Text);

}

}

Shows "TestBox1=Original text" on my system.
 
ah ha! If is do the setting in the OnPreRender method is works find, setting it in the Render method does not work. Not sure why but that is a question for another day. Thanks!
 
The Page_Render did not work because it is called after SaveViewState Event ! Page_PreRender Event is the last event called before the SaveViewState is called, so this was the right choice for you! ;-)

To refresh your memory, here is the order of the events:
- Page_Init
- LoadViewState
- LoadPostData
- Page_Load
- RaisePostDataChangedEvent
- RaisePostBackEvent
- Page_PreRender
- SaveViewState
- Page_Render
- Page_UnLoad

For people not understanding your problem, I had the same problem with a readonly textbox. Because the asp.net calendar control is uneficient in a form, I have put on a form a textbox with an image that onClick, opens a window with the calendar control and when you click on the day, it returns to the window opener the day (in the textbox) and finally close the window. Doing this, I had to put readonly the textbox field to ensure people will not put any junk in this and to avoid using a javascript function validation. So at the posting of the form I had the same problem.

To correct my problem, I made an hidden field with the same value and when my textbox was updated this field was automatically too. When submitted, my form used the hidden field value instead of the textbox.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top