radiobutton.checked always is false after postback - c#

I have 3 radiobutton control and a submit button in my aspx page which are inside a Content control.
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:RadioButton ID="rd0" runat="server" GroupName="g1" Checked="true" />
<asp:Image ID="img0" runat="server" ImageUrl="/image/img0.png" />
<asp:RadioButton ID="rd1" runat="server" GroupName="g1" />
<asp:Image ID="img1" runat="server" ImageUrl="/image/img1.png" />
<asp:RadioButton ID="rd2" runat="server" GroupName="g1" />
<asp:Image ID="img2" runat="server" ImageUrl="/image/img2.png" />
</asp:Panel>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:Content>
The user can select one of these radiobuttons and press submit button.
Now in code behind i want to diagnose which radiobutton is selected,
RadioButton checkedButton;
if (rd0.Checked)
checkedButton = rd0;
else if (rd1.Checked)
checkedButton = rd1;
else
checkedButton = rd2;
but the checked property of all of the radiobuttons is false.
I have even set the checked property of the first radiobutton to true but it becomes to false after postback again.

Use this in your .cs:
Request.Form["YourRadioButton"]

Do you have PostBack control in your Page_Load code section?
Try to add following code to your Page_Load event.
if(IsPostBack) return;

Related

printing a page after checking validator

i want that on button click it check first validator then print command is executed
here is my ASPX Code:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<style type="text/css">
.style1
{
}
#media print
{
.header, .style1, .footer,.hide
{
display:none
}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RequiredFieldValidator" style="color: #FF0000"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="window.print();" />
</asp:Content>
problem is that when i click on button it execute the print function and dont check validator.
The JavaScript OnClientClick will fire before your page is validated. You could try validating the page before printing it.
function Validate() {
if(Page_ClientValidate())
window.print();
}
Then call it like so.
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();" />
Set the CausesValidation Property of the button to true!

asp.net how to close a dialoge box using intrapage linking in asp

I made a dialog box showing wrong password or username error but this doesn't close.
how to close this dialog box without refreshing the page,Code is like
<asp:Panel ID="errorMsg" runat="server" Visible="false">
<asp:Label ID="msg" Text="" runat="server"></asp:Label>
<asp:Label ID="errorHead" Text="Something is really Wrong :" runat="server"></asp:Label>
<asp:button ID="try" OnClick="try_Click" runat="server" Text="Try Again" />
</asp:Panel>
Code Behind :
protected void try_Click(object sender, EventArgs e)
{
errorMsg.Visible = false;
}
errorMsg.visible is set to true when wrong input is made but after making it again false it doesn't close.
The OnClick event will fire a PostBack refreshing the page.
You could try using javascript:
<script text="text/javascript">
function hide(id){
if(id)
document.getElementById(id).style.display = "none";
}
</script>
<asp:Button ID="try" OnClientClick='hide("<%=errorMsg.ClientId%>")'
runat="server" Text="Try Again" />
Or you could simply wrap the dialogbox with an Update Panel. The Update Panel allows you only to refresh a part of your website.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="errorMsg" runat="server" Visible="false">
...
<asp:Button ID="try" OnClick="try_Click"
runat="server" Text="Try Again" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

AspxCallback is not updating information

I have an AspxCallback control that is supposed to update textbox text when i click the Button. But nothing happens when I click the button.
Here is my sample code for the test:
C#:
protected void callback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
{
txtTest.Text = "Text for Textbox";
}
ASP.NET:
<asp:Button ID="btnTest" runat="server" Text="CLICK" OnClientClick="callback.PerformCallback(); return false;" />
<br />
<asp:TextBox ID="txtTest" runat="server" Width="200" Height="25"></asp:TextBox>
<dx:ASPxCallback ID="callback" runat="server" ClientInstanceName="callback"
oncallback="callback_Callback">
</dx:ASPxCallback>
"Your problem resides on the fact that the TextBox is not inside a CallBack Panel.
The way a callback works is like an ajax call that can update only the Ajax enabled so to say controls. Those controls can be put inside a callback panel for this exact reason.
<dxcp:ASPxCallbackPanel ID="ASPxCallbackPanel1" runat="server" Width="223px" BackColor="#FFFFC0" ClientInstanceName="callbackPanel1" Height="78px" oncallback="callback_Callback">
<PanelCollection>
<dxp:panelcontent runat="server">
<asp:Button ID="btnTest" runat="server" Text="CLICK"
OnClientClick="callbackPanel1.PerformCallback(); return false;" />
<br />
<asp:TextBox ID="txtTest" runat="server" Width="200" Height="25"></asp:TextBox>
</dxp:panelcontent>
</PanelCollection>
</dxcp:ASPxCallbackPanel>
I think this will solve your problem. Now your code will update the TextBox properly.

very simple c# submit button

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either
TOP of the page
<div style="position:absolute; left: 70% ; top: 5%;">
<asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg" PostBackUrl="http://www.facebook.com/832586561" />
<asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />
<asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />
<asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />
</div>
Middle of Page
<div ID="formPanel" runat="server" style="position:absolute; top:235px;">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
</div>
You may set the default button via <form/> attribute.
<form defaultbutton="btnChangeCity" id="form1" runat="server">
...
</form>
Or use Panel control to set default button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>
You surround your controls with <div> which is a good idea, but to have it run at all you also need <form>. In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.
Read more here.

How to make clicking on an <asp:Panel> (or a simple <div>) a trigger event for an UpdatePanel

I have an UpdatePanel in a Repeater.
There are a few CheckBoxes in the UpdatePanel with AutoPostBack="true"
There is a Label in the UpdatePanel. I set the Text value of the label in RepeaterName_ItemDataBound as it runs on every item generated.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource" OnItemDataBound="R1_ItemDataBound">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Label ID="DateTimeLabel2" runat="server" Text="Label"></asp:Label>
<asp:Panel ID="panID" CssClass="actionicon_normal actionicon_compare" runat="server">
<%#XPath("ID")%>
<asp:CheckBox ID="chkID" runat="server" AutoPostBack="true" />
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
I want the CheckBoxes to automatically update the UpdatePanel as there were no repeater around, but possibly because the OnItemDataBound does not fire on every AsyncPostBack, nothing gets updated.
What is the proper way to do this?
In the onclick (JavaScript) call this function __doPostBack('idOfUpdatePanel', '');
Javascript and UpdatePanel

Categories