ASP.NET Textbox AutoPostBack Not Firing - asp.net

After searching I've found a number of suggestions, but none of them are fixing the issue.
<asp:TextBox runat="server" ID="uid" AutoPostBack="True" Text=""></asp:TextBox>
In the properties window, EnableViewState = True for the TextBox (Suggested here). I am typing a new value into the TextBox and then hitting the Tab key. Nothing happens nor does the break point at if(IsPostBack...) break.
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack && uid.Text != "" && pw.Text == "")
{
Do stuff
}
}
UPDATE: Other TextBox setups I've tried:
<asp:TextBox runat="server" ID="uid" Text="" AutoPostBack="True" OnTextChanged="UidTextChanged"></asp:TextBox>
protected void UidTextChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('it works');", true);
}
And
<asp:TextBox runat="server" ID="uid" AutoPostBack="True" Text="" onblur="__doPostBack('','');" OnTextChanged="UidTextChanged"></asp:TextBox>
And
<asp:TextBox runat="server" ID="uid" AutoPostBack="True" Text="" onblur="__doPostBack('','');"></asp:TextBox>
Whenever AutoPostBack is set to true, I receive the following error in the browser console:
"Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function
__doPostBack
(anonymous function)"
When I have the onblur property set, I receive the exact same error except instead of anonymous function it says onblur.

You could add a javascript event to the onblur for it. onblur='__doPostBack('','');'
That would cause your text box to cause a postback once it is tabbed out of.
Edit: It should be " not ' <asp:TextBox ID="TextBox1" runat="server" onblur="__doPostBack('','');" /> Just tried that and it works. Try removing the AutoPostBack="True"
Edit 2: Base on your pastebin....
<asp:Button runat="server" UseSubmitBehavior="True" ID="submit" Text="Save"
onclick="SubmitClick"/>
You can't have an ID of "submit". Change that to "btnSubmit" and the Javascript solution will work and I bet the Auopostback solution will too.
http://www.xpertdeveloper.com/2012/05/property-submit-of-object-is-not-a-function/ will explain the problem.

You can add OnTextChanged="TextBox1_TextChanged" on your textBox
Nota : It's important to set event fire, no just AutoPostBack="true".
<asp:TextBox runat="server" ID="uid" AutoPostBack="True" Text="" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
Code Behind:
protected void TextBox1_TextChanged(object sender, System.EventArgs e) {
.....
}

Related

Asp.net C# - checkbox control won't stay true after code-behind runs

Front HTML
<asp:CheckBox ID="chkSend" runat="server" Text="Send?" />
The code that triggers the code behind
<asp:DropDownList ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
runat="server" DataValueField="Id" DataTextField="Status" />
The code that runs in the code behind
protected void ddlStatus_SelectedIndexChanged(object s, EventArgs e)
{
this.chkSend.Checked = True;
}
When get back to the front end , the checkbox isn't checked. Any Ideas?
If it helps, this particular view is using Multi-views to which I'm new to.

Conditionally show hide asp linkbutton

I have below link button and I need to show it only if grid has records.
<asp:LinkButton ID="linkButton1" runat="server" ToolTip="Delete file" Visible='<%# (Convert.ToInt32(Eval("gridViewFileInformation.Rows.Count"))>0) %>' >Delete</asp:LinkButton>
But it shows always.
In your code behind file after databind of "gridViewFileInformation" you can set the Visible property of linkButton1. Similar question
Something like:
ASPX File:
<asp:GridView
runat="server"
ID="gridViewFileInformation"
OnDataBound="gridViewFileInformation_DataBound">
</asp:GridView>
CS File:
protected void gridViewFileInformation_DataBound(object sender, EventArgs e)
{
linkButton1.Visible = (gridViewFileInformation.Rows.Count > 0);
}

when i try to check if a radio button is checked, the code doesnt execute

i am trying 2 make a quiz in asp.net.
the mcq choices r displayed using radio buttons. in code behind, when i try 2 check if radiobutton is checked, d code under that if statement does not execute.
aspx code:
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ques") %>'></asp:Literal><br />
<asp:RadioButton GroupName="a" ID="RadioButton1" Text='<%#Eval("ch1") %>' runat="server" /><br />
<asp:RadioButton GroupName="a" ID="RadioButton2" Text='<%#Eval("ch2") %>' runat="server" /><br />
<asp:RadioButton GroupName="a" ID="RadioButton3" Text='<%#Eval("ch3") %>' runat="server" /><br />
<asp:RadioButton GroupName="a" ID="RadioButton4" Text='<%#Eval("ch4") %>' runat="server" /><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ans") %>' Visible="false"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
</ItemTemplate>
code behind:
protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;
foreach(RepeaterItem Items in Repeater1.Items)
{
RadioButton r1 = (RadioButton)Items.FindControl("RadioButton1");
RadioButton r2 = (RadioButton)Items.FindControl("RadioButton2");
RadioButton r3 = (RadioButton)Items.FindControl("RadioButton3");
RadioButton r4 = (RadioButton)Items.FindControl("RadioButton4");
Label l3 = (Label)Items.FindControl("Label3");
Label l=(Label)Items.FindControl("Label1");
l3.Text = "hello?";
if (r1.Checked)
{
if(r1.Text==l.Text)
count++;
}
else
{
if (r2.Checked)
{
if(r2.Text==l.Text)
count++;
}
}
// and so on for all 4 options
}
Label2.Visible = true;
Label2.Text = "your score is " + count; //always zero!
}
If you're stepping through the debugger, and your line
if(r1.Text==l.Text)
count++;
isn't executing, then I would guess that the line if (r1.Checked) is evaluating to false.
On your Page_Load() method for this page, are there any databindings or manipulation of these radio buttons? If so, unless you wrap them in a if(!Page.IsPostBack){ ... } conditional then it will wipe out whatever the user did to the radio button, hence r1.Checked would be false.
I hope that might help :) Good luck.
You'll need to do the following:
Set the AutoPostBack on the radio button control to true.
Set the OnItemCommand on the repeater control to "Button1_Click"
Change the signature of the Button1_Click method to protected void Button1_Click(object sender, RepeaterCommandEventArgs e)
That will at least trigger the method in your code behind.

how to disable server side validation on asp.net web forms from browser?

I wanted to disable asp.net validation server controls from browser. I checked online but did not find any way to disable the server side validation; it can be disabled only on the client side using JS/jQuery.
Here is the scenario: I have a checkbox and selecting which displays a set of text boxes. Only if the checkbox is checked, required field validator should fire for the text boxes. I don't want to call a postback on checkbox. Actually those chceck boxes will be generated with jQuery templating so postback is not an option to enable disable validtion.
I would like to know whether there is any way we can enable disable the .CausesValidation property for the controls from browser using some setting? Or is there a way to capture the controls which are to be considered for validation slectively in some event before page_load?
[Update]
Based on Accepted answer, here is my solution:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="req1" ControlToValidate="textbox1" runat="server"
ErrorMessage="enter text"></asp:RequiredFieldValidator>
<asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="req2" ControlToValidate="textbox2" runat="server"
ErrorMessage="enter text for 2"></asp:RequiredFieldValidator>
<asp:CheckBox ID="check1" runat="server" Text="choose" />
<asp:Button ID="submitBtn" runat="server" OnClick="submitBtn_Click" Text="submit" />
<asp:CustomValidator ID="cvBox" runat="server" ErrorMessage="Error" ValidationGroup="prueba"
OnServerValidate="Validarcaja"></asp:CustomValidator>
<asp:ValidationSummary ID="summary" runat="server" />
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
req1.Enabled = false;
req2.Enabled = false;
}
protected void submitBtn_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Page.Validate();
if (Page.IsValid)
{
Response.Write("valid form");
}
else
{
Response.Write("invalid form");
}
}
}
protected void Validarcaja(object source, ServerValidateEventArgs args)
{
if (check1.Checked)
{
req1.Enabled = true;
req1.Validate();
}
}
The solution for me would be to use a CustomValidator with a OnServerValidate method.
In the OnServerValidate method I would check if the checkbox is checked, in that case I would verify if the textboxes are filled. It is not necessary to do any change in the CausesValidation property.
The only condition is not to include the property "ControlToValidate". A CustomValidator does not fire if the textbox is empty that's why.
So the code would be like this:
<asp:ValidationSummary ID="vs" runat="server" ValidationGroup="prueba" />
<asp:CheckBox ID="chb" runat="server" Text="Check" />
<asp:TextBox ID="txbBox" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvBox" runat="server" ErrorMessage="Error" ValidationGroup="prueba"
OnServerValidate="Validarcaja"></asp:CustomValidator>
<asp:Button ID="btn" runat="server" Text="Prueba" />
And the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Page.Validate();
}
}
protected void Validarcaja(object source, ServerValidateEventArgs args)
{
if (chb.Checked)
{
if (txbBox.Text == String.Empty)
{
cvBox.IsValid = false;
}
}
}

UpdatePanel with ASP.NET Repeater and Checkbox Aync Postback issue

I have a rather annoying issue here
I can't get my CheckBox CheckedChange event to fire, or catch or whatever it is that fails:
ASPX Code
<asp:UpdatePanel runat="server" ID="udp_Lists" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rep_showings" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="div_assignment">
<div class="div_assignment_text">
<asp:LinkButton runat="server" ID="lnk_show_task" OnClick="lnk_show_task_Click" CommandArgument='<%# Eval("Id") %>' Text='<%# Eval("TaskTitle") %>'></asp:LinkButton>
</div>
<div class="div_assignment_checkbox">
<asp:CheckBox runat="server" ID="chk_handle" AutoPostBack="true" OnCheckedChanged="chk_handle_Changed" ToolTip='<%# Eval("Id") %>' />
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
</Triggers>
The Code behind function "chk_handle_Changed" is never reached.
The Linkbutten works perfectly.
I took a look at your problem. I used the following code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.rep_showings.DataSource = new object[] { new { Title = "title", ID = "id" } };
this.rep_showings.DataBind();
}
}
protected void chk_handle_Changed(object source, EventArgs e)
{
Trace.Write("here");
}
protected void lnk_show_task_Click(object source, EventArgs e)
{
Trace.Write("here 2");
}
protected void rep_showings_ItemCommand(object source, RepeaterCommandEventArgs e)
{ }
The above code works. I think you are probably re-binding your repeater on every postback - I tested this by removing the "if (!IsPostBack)" statement in Page_Load(), and I was able to reproduce the problematic behaviour you describe.
Rebinding a control on every postback should be avoided if possible. Once a control is populated, it's data is taken care of by ViewState, so unless the data is changing, you should probably not be rebinding it all the time.

Resources