Using WebForms, how can I have a button, which is disabled, after only IT has been clicked, and also AFTER validation has been passed?
I know there is: Disable asp.net button after click to prevent double clicking
However, there are other postbacks on my page, before the final button press which confirms the booking, and I need these to be allowed.
It's only the final button press that I need to ensure passes validation, but only allows one press, before being disabled.
Code is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FinalButton.Attributes.Add("onclientclick", "if(Page_ClientValidate('vg')){this.disabled = true; }");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Hi";
}
protected void Button3_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
ASPX page:
<p>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="vg"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Set TextBox1 to Hi" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Set TextBox1 to Empty" />
</p>
<p>
<asp:Button ID="FinalButton" runat="server" Text="Final Submit" ValidationGroup="vg" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" ValidationGroup="vg"></asp:RequiredFieldValidator>
</p>
So, the unless the textbox is populated, the "FinalButton" should not submit (this part works). However, if the textbox is populated, then it should be disabled to prevent double clicks, before posting back - but it doesn't disable, so I presume my code here, is not correct:
FinalButton.Attributes.Add("onclientclick", "if(Page_ClientValidate('vg')){this.disabled = true; }");
The source on the page when it is rendered, shows the button as:
<input type="submit" name="ctl00$MainContent$FinalButton" value="Final Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$FinalButton", "", true, "vg", "", false, false))" id="MainContent_FinalButton" onclientclick="if(Page_ClientValidate('vg')){this.disabled = true; }" />
Thank you,
Mark