This is a migrated thread and some comments may be shown as answers.

Raise RadButton Click Event in C#

1 Answer 551 Views
Button
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 20 Dec 2011, 03:43 PM
I'm working with the RadButton, encapsulated within an asp:Login container;

An application scenario includes the requirement of programmatically accessing textbox variables, and the RadButton;

All is OK, until the C# code attempts to process the button click event, programmatically;

Object reference not set to an instance of an object results on the line of: ((IPostBackEventHandler)submitButton).RaisePostBackEvent(null);

Any insight is appreciated;  Best regards - Rob

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
        <LayoutTemplate>
            <span class="failureNotification">
                <asp:Literal ID="FailureText" runat="server"></asp:Literal>
            </span>
            <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
                 ValidationGroup="LoginUserValidationGroup"/>
            <div class="accountInfo">
                <fieldset class="login" style="border-style: none; border-color: Transparent">
                  <%--  <legend>Account Information</legend>--%>
                    <p>
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="160px" AutoPostBack="true"
                            ontextchanged="UserName_TextChanged"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                             CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                            Width="160px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                             CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                       
                    </p>
                </fieldset>
                <p class="submitButton">
  
                     <telerik:RadButton ID="rbLogin" runat="server" CommandName="Login" 
                         Text="Log In" ValidationGroup="LoginUserValidationGroup" 
                         Font-Names="Microsoft Sans Serif" Skin="Black">
                    </telerik:RadButton>
  
                       <telerik:RadButton ID="rbForgotPassword" runat="server" 
                         Text="Forgot Password" ValidationGroup="LoginUserValidationGroup" 
                         Font-Names="Microsoft Sans Serif" Skin="Black" 
                         onclick="rbForgotPassword_Click">
                    </telerik:RadButton>
  
                </p>
            </div>
        </LayoutTemplate>
    </asp:Login>

string

 

 

obtainUsername = Request.QueryString["Username"];

 

 

 

string obtainPassword = Request.QueryString["Password"];

 


var userNameTextBox = LoginUser.FindControl("UserName") as TextBox;
var passwordTextBox = LoginUser.FindControl("Password") as TextBox;

userNameTextBox.Text = obtainUsername;

passwordTextBox.Text = obtainPassword;


var submitButton = LoginUser.FindControl("rbButton") as RadButton;
  
((IPostBackEventHandler)submitButton).RaisePostBackEvent(null);


1 Answer, 1 is accepted

Sort by
0
Accepted
Slav
Telerik team
answered on 23 Dec 2011, 01:39 PM
Hello Robert,

The object reference exception is thrown because the RadButton cannot be located in the ASP Layout control. Note that you have set the ID property of the button control with value rbLogin, but on the server-side the RadButton is searched with the string rbButton. Please change it to rbLogin in order to resolve the issue at hand. In the code sample below you can check the correct setup:

Page:
<telerik:RadButton ID="rbLogin" runat="server" CommandName="Login"
     Text="Log In" ValidationGroup="LoginUserValidationGroup"
     Font-Names="Microsoft Sans Serif" Skin="Black">
</telerik:RadButton>

Code-behind:
protected void UserName_TextChanged(object sender, EventArgs e)
{
    string obtainUsername = Request.QueryString["Username"];
    string obtainPassword = Request.QueryString["Password"];
    var userNameTextBox = LoginUser.FindControl("UserName") as TextBox;
    var passwordTextBox = LoginUser.FindControl("Password") as TextBox;
 
    userNameTextBox.Text = obtainUsername;
 
    passwordTextBox.Text = obtainPassword;
 
    var submitButton = LoginUser.FindControl("rbLogin") as RadButton;
 
    ((IPostBackEventHandler)submitButton).RaisePostBackEvent(null);
 
}


All the best,
Slav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Button
Asked by
Robert
Top achievements
Rank 1
Answers by
Slav
Telerik team
Share this question
or