Ajax ModalPopUp Window according to certain conditions - c#

I have a Ajax ModalPopupExtender in my page.On a button click currently using TargetControlId i am showing the pop up.My need is when i am clicking the button want to check some conditions from DB.If condition satisfies i want to show the pop up.Other wise no need of pop up.How can i do this?
<ajaxToolkit:ModalPopupExtender CancelControlID="btnCancel" BackgroundCssClass="modalBackground" runat="server" ID="PopupExtender" TargetControlID="btn" PopupControlID="Panel1"></ajaxToolkit:ModalPopupExtender>

protected void ButtonSave_Click(object sender, EventArgs e)
{
if (MyCondition == true)
{
modalPopUpConfirmation.Show();
}
else
{
Label1.Text = "The condition was false, so no modal popup!";
}
}

Try like this
protected void Button1_Click(object sender, EventArgs e)
{
PopupExtender.Show();
}

Call it dynamically.
<!-- Hidden Field -->
<asp:HiddenField ID="hidForModel" runat="server" />
<ajaxToolkit:ModalPopupExtender CancelControlID="btnCancel" BackgroundCssClass="modalBackground" runat="server" ID="PopupExtender" TargetControlID="hidForModel" PopupControlID="Panel1"></ajaxToolkit:ModalPopupExtender>
<asp:Button ID="btnShowPopup" runat="server" Text="Save Data"
OnClick="btnShowPopup_Click" />
Code Behind
protected void btnShowPopup_Click(object sender, EventArgs e)
{
if(Yourcondition)
{
PopupExtender.Show();
}
}

Related

CustomValidator firing but not not preventing postback in ASP.NET

I am using a CustomValidator in ASP.NET as follows.
<asp:CustomValidator ID="AnswerCV" runat="server" ForeColor="Red"
ValidateEmptyText="True" ValidationGroup="test"
OnServerValidate="CustomValidation" ControlToValidate="TextBox1" Enabled="True"
ErrorMessage="You must fill in the text box.">
</asp:CustomValidator>
<asp:TextBox ID="TextBox1" ValidationGroup="test" runat="server">
</asp:TextBox>
<asp:Button runat="server" Id="Button" CausesValidation="True" ValidationGroup="test"
OnClick="Button_Click" />
In the code behind I have
protected void CustomValidation(object sender, ServerValidateEventArgs e)
{
MessageBox.Show("It is firing!!!");
if (string.IsNullOrEmpty(TextBox1.Text))
{
e.IsValid = false;
}
else
{
e.IsValid = true;
}
}
And the button click method is as follows
protected void Button_Click(object sender, EventArgs e)
{
MessageBox.Show("I should not have fired.");
}
The CustomValidation method fires but the Button_Click method fires after that and then the "You must fill in the text box." error message displays. How do I prevent the Button_Click method from firing, when the text box is empty the validation is failing and triggering the error message, but after the Button_click method has fired?
The stackoverflow page here offered other solutions which I have implemented but still the Button_Click method fires.
Aside: And the client side solutions I can not use as I am dynamically going to add code in the Init() method that enables and disables the CustomValidator via a CheckedChanged event in only certain Radio Buttons. End Of Aside
I have also tried the CustomValidator without the OnServerValidate method and I have tried returning a boolean of false from the CustomValidation method cause a syntax error.
protected void Button_Click(object sender, EventArgs e)
{
if (IsValid)
{
Response.Write(" alert ('I should not have fired'); ");
}
}
Please add one if condition in Button_Click Event
protected void Button_Click(object sender, EventArgs e)
{
if (IsValid)
{
Response.Write("<script> alert ('I should not have fired');
</script>");
}
}

ASP.NET checkbox autopostback not working

I designed a simple page with, two text boxes, one checkbox, one button, and one label.
When I start I want to check the checkbox to make the button enabled, and then enter two numbers into the two textboxes, click the button to do addition, and show the result in the label.
But when I click the checkbox the page postback is not working; it's not writing Page is posted back on the page and the button is still disabled.
However, if I make the button enabled and do the addition it invokes the page postback and also invokes the checkedchanged method.
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<td>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
</td>
<asp:Button ID="submit" runat="server" Text ="addition" onclick="Button_Click"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
submit.Enabled = cboptions.Checked;
}
protected void submit_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text)+a;
result.Text = b.ToString();
}
There were many formatting errors in your code, do it this way
Aspx
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
<asp:Button ID="btn" runat="server" Text ="addition" onclick="Button_Click"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
btn.Enabled = cboptions.Checked;
}
protected void Button_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text) + a;
result.Text = b.ToString();
}

How to get Text property of TextBox after ItemCommand event

I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}

Issue with radio buttons while firing Validations

Hi all i have my form with some controls as follows
2 Radio buttons 1 Text Box, 1 Required field validator and a button
I wrote my sample code in such a way that if one radio button is selected i will enable or disable the text box that i will have.
I am having a required field validator which was set for text box available. Now what i need is when the control was disabled i don't want to perform the validation for this is it possible to do
Sample code
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = true;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = false;
}
My design
<form id="form1" runat="server">
<div>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="g"
OnCheckedChanged="RadioButton1_CheckedChanged" ValidationGroup="g1" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="g"
OnCheckedChanged="RadioButton2_CheckedChanged" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RequiredFieldValidator" ValidationGroup="g1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="g1" /></div>
</form>
Validation should apply only when the control was enabled
Validators have an Enabled property that you can use:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = RequiredFieldValidator1.Enabled = true;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = RequiredFieldValidator1.Enabled = false;
}
I got this and works well for me
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Enabled = true;
Button1.CausesValidation = true;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Button1.CausesValidation = false;
TextBox1.Enabled = false;
}

Handling control events from Repeater footer

Assuming I have the following repeater.
<asp:Repeater ID="MyRepeater" runat="server" onitemdatabound="MyRepeater_ItemDataBound">
<FooterTemplate>
</table>
<asp:Button ID="btnPrevious" runat="server" Text="<" />
<asp:Label ID="lblCurrentPage" runat="server" Text="<%# PagingStatus() %>" />
<asp:Button ID="btnNext" runat="server" Text=">" />
</FooterTemplate>
</asp:Repeater>
How can I handle the click events from btnPrevious and btnNext?
I have tried the following:
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button btnPrevious = (Button)e.Item.FindControl("btnPrevious");
Button btnNext = (Button)e.Item.FindControl("btnNext");
if (btnPrevious != null)
btnPrevious.Click += btnPrevious_Click;
if (btnNext != null)
btnNext.Click += btnNext_Click;
}
But this has failed (The event is never raised)..
You can use them in the same way you would use a normal button event handler eg:
Html:
<asp:Button ID="btnNext" runat="server" CommandArgument="<%=Id%>" onclick="Button_OnClick" Text=">" />
Code:
protected void Button_OnClick(object sender, EventArgs e)
{
Button button = sender as Button;
if(button != null)
{
string commandArg = button.CommandArgument;
//Do Work
}
}
The you can use the command argument to find out which button was clicked.
Hope this helps.
I would suggest using the ItemCommand event of the repeater. You still have to add the commands to your buttons though. Like this:
<asp:Button ID="btnPrevious" runat="server" Text="<" CommandName="Previous"/>
protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.ToLower().Equals("previous")) {
//go back
}
else
{
//go forward
}
}

Categories

Resources