0

First time I am writing some ASP.NET code and stumbled upon a weird bug. When I click the button to post, it does so, but without any params.

Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="SingTelAPI.Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table style="width:100%;">
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <table style="width:100%;">
                    <tr>
                        <td>
                            &nbsp;</td>
                        <td align="center">
                            <asp:Label ID="Label4" runat="server" Text="Login" Font-Bold="True" 
                                Font-Names="Verdana" ForeColor="#CC0000" Font-Size="Large"></asp:Label>
                        </td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;</td>
                        <td>
                            <asp:Panel runat="server" DefaultButton="SubmitBtn" ID="Panel1">
                                <table style="width:100%;">
                                    <tr>
                                        <td class="style1">
                                            <asp:Label ID="Label9" runat="server" Font-Bold="True" Font-Names="Verdana" 
                                            ForeColor="#0066FF" Text="Inupt Details:"></asp:Label>
                                        </td>
                                        <td>
                                        &nbsp;</td>
                                        <td>
                                        &nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td class="style1">
                                            <asp:Label ID="Label5" runat="server" Text="Email:"></asp:Label>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="Email" runat="server" Skin="Default">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                            <asp:CustomValidator ID="EmailValidator" runat="server" 
                                            ErrorMessage="Please Enter Email" Font-Bold="True" Font-Names="Verdana" 
                                            ForeColor="#00CC66" ControlToValidate="Email" ValidateEmptyText="True" 
                                            ValidationGroup="LoginValidation"></asp:CustomValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="style1">
                                            <asp:Label ID="Label6" runat="server" Text="Password:"></asp:Label>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="Password" Runat="server" Skin="Default">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                            <asp:CustomValidator ID="PasswordValidator" runat="server" 
                                                ControlToValidate="Password" ErrorMessage="Please Enter Password" Font-Bold="True" 
                                                Font-Names="Verdana" ForeColor="#00CC66" ValidateEmptyText="True" 
                                                ValidationGroup="LoginValidation"></asp:CustomValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="style1" colspan="3">
                                           <hr /></td>
                                    </tr>
                                    <tr>
                                        <td class="style1">
                                            <telerik:RadButton ID="SubmitBtn" runat="server" 
                                                Skin="Default" Text="Login" ValidationGroup="LoginValidation" 
                                                Width="120px">
                                            </telerik:RadButton>
                                        </td>
                                        <td>
                                            <asp:Label ID="ResultsLabel" runat="server" ForeColor="#339933"></asp:Label>
                                        </td>
                                        <td>
                                            &nbsp;</td>
                                    </tr>
                                </table>
                            </asp:Panel>
                        </td>
                        <td>
                            &nbsp;</td>
                    </tr>
                </table>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
</asp:Content>

And the .cs file:

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string email = HttpContext.Current.Request["Email"];
            Label9.Text = email;
        }
    }
}

It changes the text of Label9 to "" for some reason. First I thought the problem was in the fact that I didn't have telerik installed, so I installed that. But it didn't change a thing.

Vadiklk
  • 3,696
  • 5
  • 28
  • 44

3 Answers3

1

I would check all the fields which are actually being post to the page as I suspect that Email is actually not Email.

In asp.net, especially with Master and Content Pages, the id's and names for inputs are dynamically changed.

So Email could become something like ctl00_ContentPlaceHolder1_Email for example.

Saying this, you shouldnt need to use the Request object to access fields posted back to a .net page. You are able to access the values directly.

e.g.

Label9.Text = this.Email.Text;
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • Won't this trick work only when posting back to the same page back? (I mean, if I would to post it to another one, shouldn't I be using Request object then?). – Vadiklk Apr 17 '12 at 08:44
  • Yeah if you are posting to another page, then you can use the Request object, but check what the field name is when it is being Post. – Tim B James Apr 17 '12 at 09:40
1

The ID of the control changes when it is being sent to the client so on PostBack the HttpContext most likely doesn't have a field with ID "Email".

Try this:

if (IsPostBack)
{
 string email = Email.Text; //Email refers to <telerik:RadTextBox ID="Email"/>
 Label9.Text = email;
}
Bazzz
  • 26,427
  • 12
  • 52
  • 69
  • I've read in another question how to used the Request object. Why does it work there? http://stackoverflow.com/questions/976613/get-post-data-in-c-asp-net – Vadiklk Apr 17 '12 at 08:36
  • because your control has `runat="server"` which means that the server can manipulate the control and uses the ViewState to maintain it's value. The way you access the value now is just by referring the control itself, in your case "Email", and access one of it's properties, in your case "Text". The example that you show uses controls that do not have `runat="Server"`. – Bazzz Apr 17 '12 at 08:38
  • Won't this trick work only when posting back to the same page back? (I mean, if I would to post it to another one, shouldn't I be using Request object then?). – Vadiklk Apr 17 '12 at 08:43
  • @Vadiklk, yes but then it's not a `PostBack` as in the classic meaning of the word => posting BACK to the page that generated the form. `PostBack` as implemented in asp.net implies posting to the same page. That's why on your Page_Load event you can see whether the page is in fact a `PostBack`. I made quite some pages in asp.net but never actually implemented a post to another page. Perhaps this is something that is not a natural behaviour in asp.net? In the end the .net framework is really made for doing PostBacks, hence the Page.IsPostBack property. – Bazzz Apr 17 '12 at 12:57
1

It seems that you haven't attached the event-handler of your button, so change it to:

<telerik:RadButton ID="SubmitBtn" runat="server"  Skin="Default" 
    OnClick="RadButton1_Click" Text="Login" ValidationGroup="LoginValidation"  Width="120px">
</telerik:RadButton>

and handle it in codebehind:

protected void RadButton1_Click(Object sender, EventArgs e)
{
    String email = Email.Text;
}

Remove the part in Page_Load since it's redundant.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Do I have to add an event-handler? Can I post back and use it inside my page_load function? – Vadiklk Apr 17 '12 at 08:41
  • @Vadiklk: You can, but it's not good practise since you're not _handling_ but _assuming_ a user-action what is error-prone and moreover not very clear. That's why event-handler exist. Use Page_Load with a `!IsPostBack`-check to initialize your default form only. – Tim Schmelter Apr 17 '12 at 08:44
  • So, what would the event do? It will declare the "email" to be the email we've gotten. But then will it reload the page and let this function decide what to do with the post data? – Vadiklk Apr 17 '12 at 08:47
  • @Vadiklk: Your question is about the button's click event("When I click the button..."). So you should handle it, there you can access the `Text` property of the TextBox with ID `Email`. This is what you actually want. No need to look into the `NameValueCollection` of `Request`. Consider that you're renaming controls in future, then that wouldn't work anymore and you'll get runtime exceptions and not compiletime. – Tim Schmelter Apr 17 '12 at 08:52
  • @Vadiklk: Yes, you would just set the Email-Text to the same value as before, not very useful. But what do you actually want? – Tim Schmelter Apr 17 '12 at 08:55
  • I want to check if the email and password are in my database. – Vadiklk Apr 17 '12 at 08:59
  • @Vadiklk: Then add that to the click-event handler provided in my answer. – Tim Schmelter Apr 17 '12 at 09:04