Different result when adding same attributes to asp control? - c#

So I have this method that should run on TextChanged of a text box:
void CheckIn_TextChanged(object sender, EventArgs e)
{
checkIn.Text += "It Worked!";
}
In the aspx file I have this control:
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" AutoPostBack="true" TextChanged="CheckIn_TextChanged"></asp:textbox>
All the attributes work as they should except the TextChanged?
But if I remove this from the control and set it in the codebehind on page_load like so: checkIn.TextChanged = CheckIn_TextChanged; it does work?!
So my question is this why does it work when setting in codefile behind but not assigning the attribute to the control in the aspx file? Where am I going wrong?

Event name should be OnTextChanged. (Not TextChanged)
<asp:TextBox runat="server" ID="checkIn"
ClientIDMode="Static"
AutoPostBack="true"
OnTextChanged="CheckIn_TextChanged">
</asp:TextBox>

Related

Bind control's Enable property to a CheckBox's Checked property

I want to set the Enable property of a RequiredFieldValidator control, depending on the Checked property of a CheckBox control. My controls are wrapped in an UpdatePanel. If I write the following code, everything works fine.
ASPX/HTML:
<asp:CheckBox
ID="chkIsEmailSubscribed"
runat="server"
OnCheckedChanged="chkIsEmailSubscribed_CheckedChanged"
AutoPostBack="true" />
<asp:TextBox
ID="txtSubscriptionEmail"
runat="server" />
<asp:RequiredFieldValidator
ID="rqrSubscriptionEmail"
runat="server"
ControlToValidate="txtSubscriptionEmail"
ErrorMessage="Email is required" />
Code-behind:
protected void chkIsEmailSubscribed_CheckedChanged(object sender, EventArgs e)
{
rqrSubscriptionEmail.Enabled = chkIsEmailSubscribed.Checked;
}
But I want to achieve this without writing any code in the code-behind and instead doing it in the HTML. I want to replace my code-behind logic with the following binding expression:
<asp:RequiredFieldValidator
...
Enabled="<%#chkIsEmailSubscribed.Checked%>" />
But this binding expression doesn't work as I expected. What's wrong with this?
I suggest you try to display the value of <%#chkIsEmailSubscribed.Checked%> from your front-end output.

SelectedIndexChanged event of DropDownList is not fired

ASPX FILE contain DropDown as Follows:
< asp:DropDownList ID="drpDist" runat="server" CssClass="dropDownStyle" OnSelectedIndexChanged="drpDist_SelectedIndexChanged" TabIndex="6">
In ASPX.CS FILE
protected void drpDist_SelectedIndexChanged(object sender, EventArgs e)
{
}
Please Help me.I can't get why it is not working.
Use Property
AutoPostBack="True"
you need to set AutoPostBack="true"
<asp:DropDownList ID="drpDist" runat="server" AutoPostBack="true">
when you set that property as true, a postback to the server automatically occurs whenever the user changes the selection of the list
You need to set the AutoPostBack="True" property. This will make the page to postback automatically hence firing your event.
Set AutoPostBack="true"
< asp:DropDownList ID="drpDist" runat="server" AutoPostBack="true" CssClass="dropDownStyle" OnSelectedIndexChanged="drpDist_SelectedIndexChanged" TabIndex="6">

Databind fires after ItemUpdated event

I'm using an asp.net 4 web site project (C#) in VS2008 and I have a FormView with ItemUpdated event:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
FormView1.DataBind(); // adding this line even doesn't help
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
box.Enabled = false;
}
But I can't figure out, why an extra "FormView1.DataBind()" or Render(?) happens AFTER the ItemUpdated event. The result is that my code in the ItemUpdated event gets like "overwritten" and the TextBox1 doesn't get disabled.
When I set a breakpoint to the last line "box.Enabled = false;" then I see that after the ItemUpdated event it jumps to the aspx page again and steps through the TextBoxes.
Disabling this TextBox1 from another GridView1_SelectedIndexChanged works fine.
Is there any way to see the "current lifecycle progress" in debugging?
EDIT:
To clarify the reasoning...
I have a GridView1 where selecting the item populates the abovementioned FormView1. The point is that I need to disable some of the TextBoxes in the FormView1 based on, for example, user access levels.
Selecting the item from GridView1 disables the TextBox1 just fine, but when I click the Update button on the FormView1 then all TextBoxes are enabled, even if I see in the debugger code running through the GridView1_SelectedIndexChanged() function. And after I re-select the gridview item, the correct TextBoxes are disabled again.
Using even this code:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" DefaultMode="Edit" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("col2") %>' />
<asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:FormView>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
GridView1_SelectedIndexChanged(sender, e);
}
Maybe I should disable my Textboxes via another event?
This doesn't make sense, please explain more about why you are trying to disable the TextBox, have you just left the ItemTemplate off in your question? or is it actually missing? if it's missing why?
The TextBox is in the FormView's EditItemTemplate, so will only be visible when the FormView is in edit mode. After clicking update or cancel the TextBox will no longer be rendered, and the ItemTemplate is rendered instead. So there should be no need to set the TextBox to be disabled.
EDIT
Ok since you edited your question. You need to use the OnDataBound event of the FormView, which occurs at the end of binding, and disable your TextBoxes at that point.
aspx
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1"
OnDataBound="FormView1_DataBound">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
aspx.cs
protected void FormView1_DataBound(object sender, EventARgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
Rather then using gridview selected Index change you can use DataBound event on formview, so your logic would fire everytime the formview is rebind.

Value is not found in boundfield in gridview?

I am trying to assing value to hiddenfield on indexchange event of dropdownlist ! Actually the problem is when I am trying to update my record i can not find value of that hidden field ! Kindly give me solution or suggest any another option ! Thank You !
My grid view is
<asp:TemplateField HeaderText="LocCode" SortExpression="LocCode">
<EditItemTemplate>
<ajax:UpdatePanel ID="upEditsLocation" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlLocation" runat="server"
DataSourceID="sdsLocation"
OnDataBound="ddlLocation_DataBound"
DataValueField="LocCode" AppendDataBoundItems="false"
DataTextField="LocCode"
AutoPostBack="true"
onselectedindexchanged="ddlLocation_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ccConnString %>"
ProviderName="<%$ ConnectionStrings:CCConnString.ProviderName %>" SelectCommand="Select LocCode from Location">
</asp:SqlDataSource>
</ContentTemplate>
</ajax:UpdatePanel>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%# Bind("LocCode") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
and my indexchange event is
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = ddlLocation.SelectedItem.Text;
}
And my hidden field is
<asp:HiddenField ID="hdloc" runat="server" />
From the code I can see the HiddenField is not part of your update panel. Hence if you assign any value to it, it will not reflect on client machine. Increase the scope of panel to include the hidden field, and then try.
OR you can try this solution from ASP.net Forum
Here is a small tutorial on update panel (MSDN)
Hope this helps you.
GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)cancel.FindControl("lblid");
If you can't access hdloc from code behind, either is not added by Visual Studio on aspx.designer.cs (try delete it and add it back or change the id and then back to original value) or the hidden field is placed in other template of another binding control, which means you need to use ctrl.FindControl("hdloc") then cast to HiddenField.
Also you need to place this hidden field into an UpdatePanel with UpdateMode="Always".
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = (sender as DropDownList).SelectedItem.Text;
}
I'm sure that ddlLocation.SelectedItem.Text, like you use it, it gives a compilation error, because ddlLocation is not visible on code behind, since is inside of EditItemTemplate.

How can i access a control within a ListView once a button has been clicked?

I need to access a label control in a listview when I've clicked a button (that is on the same row)...
Does anyone know how to do this please? :(
See below for more of an insight...
ASPX Page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
Code Behind:
protected void voteOnThis(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Voting.vote(int.Parse(myButton.CommandArgument));
// Here i would like to access the 'label' lblDone and make this Visible
}
In this simple case, I should consider using Javascript (JQuery)
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" style="visibility:hidden">Your vote has been counted</asp:Label>
<asp:Button OnClientClick="showLblDone()" ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
now, define inside a script tag the showLblDone function:
<script>
function showLblDone (){
$(this).siblings('span').show();}
</script>
You can also call this function with a parameter if you want to show/hide on every click, or you can use .toggle() instead of .show().
In this case you must add a div (or a Panel) inside the ItemTemplate.
You need to hook into the listview row bind and add the information you want to have when clicked. Using this, you can add an attribute to the button that you read back on click, for example...
If you posted some actual code, I could probably help some more.

Categories