HtmlInputCheckbox onselect event not getting invoked - c#

I have a web application which has a gridview with first column as checkbox.
<asp:TemplateField HeaderText="Select" >
<HeaderTemplate >
<asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<input id="chkSelect" name="chkboxSelect" runat="server" type="checkbox" onclick='CheckedChanged'/>
</ItemTemplate>
</asp:TemplateField>
And also there is a onselect event for input(checkbox) written in code behind where I will highlight the row based on whether the checkbox is checked or not.
My problem is when I check the input checkbox, the onselect event is not getting invoked. Can anyone know whats causing this issue?
Thanks in advance.

Use asp:checkbox with autopostback=true instead of HTML control

use this code.
replace html input to asp:checkbox on itemtemplate and assign new event OnCheckedChanged instead of onclick event
<asp:TemplateField HeaderText="Select" >
<HeaderTemplate >
<asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged='CheckedChanged'/>
</ItemTemplate>

Related

Asp.net checkbox renders span with AutoPostBack = true in it

This has got me at my wit's end. I have a GridView with a column with a checkbox in it. The checkbox is set to call CheckedChanged and AutoPostBack is true.
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbNotifier" runat="server" OnCheckedChanged="cbNotifier_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
I am using similar code in the first column of the GridView and it works perfectly. I am using the code above in a different column. It does not work. Instead of rendering the checkbox to do a postback, it renders a span around the checkbox like this:
<span AutoPostBack="true"><input id="cbNotifier" type="Checkbox"></span>
so, of course, the checkbox will not postback.
If I add another, identical checkbox to the same column, like this ...
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbNotifier" runat="server" OnCheckedChanged="cbNotifier_CheckedChanged" AutoPostBack="true" />
<asp:CheckBox ID="cbNotifier1" runat="server" OnCheckedChanged="cbNotifier_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
... the second checkbox postsback perfectly, the first one gets the span tag around it. What on earth is going on?

button in gridview triggers all validators when it should not

Let me start off by saying I know how to use CausesValidation and ValidationGroup. I have bizarre situation where I have an Ajax Tab container. Each tab has a gridview and the rows that generate in the gridviews all have a TemplateField containing an 'Add' button. The buttons in the gridview and tab containers all use ValidationGroup to prevent any cross contamination and all the 'Add' buttons are set to CausesValidation = "false"
I just added the last gridview and the 'Add' button triggers all validators on the page. Once more the 'Add' button is set as CausesValidation = "false" so there is NO reason for it to behave in this fashion. I even set this value on the front end AND in the code behind.
For attempts I have tried the suggestions in these links: "Solution 1" and "Solution 2"
Has anyone else come across this and found a solution?
Edit Request for code:
I will post only the Tab Panel in question as the whole container is almost 500 lines
<asp:TabPanel ID="tab_Browse" runat="server" HeaderText="Browse">
<ContentTemplate>
<table id="browseTable">
<tr>
<td>Category</td>
<td>
<asp:DropDownList ID="browseDDL" runat="server" AppendDataBoundItems="True">
<asp:ListItem Text="All categories" Value="999"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Button ID="btnBrowse" runat="server" Text="Browse" ValidationGroup="vgBrowse" OnClick="btnBrowse_Click" />
</td>
</tr>
</table>
<asp:GridView ID="gvBrowse" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="8"
OnPageIndexChanging="gvBrowse_PageIndexChanging" DataKeyNames="foodId"
OnRowDataBound="gvBrowse_RowDataBound" Width="790px">
<Columns>
<asp:TemplateField HeaderText="Food ID" Visible="false">
<ItemTemplate>
<%# Eval("foodId") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("Shrt_Desc") %> (100g)
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Calories" ItemStyle-Width="50px">
<ItemTemplate>
<%# Eval("cals") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fat Grams" ItemStyle-Width="75px">
<ItemTemplate>
<%# Eval("fat") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Carbs" ItemStyle-Width="50px">
<ItemTemplate>
<%# Eval("carbs") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="110px">
<ItemTemplate>
<asp:Button ID="browseAdd" runat="server" CausesValidation="false" Text="Add to meal" CommandArgument="Standard" OnClick="showingEntry" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gvRowStyle" />
<HeaderStyle CssClass="gvHeader" />
<AlternatingRowStyle CssClass="altRow" />
</asp:GridView>
</ContentTemplate>
</asp:TabPanel>
Edit 2 Another interesting tidbit. If I put OnClientClick="alert(Page_IsValid)" on my add button it returns true even though the validators in the others tabs are triggered. if i click the button a second time it returns false and my codebehind fails as if the page posted back but skipped the OnDataBound method
I ended up resolving this problem by preloading the gridview with an empty row. Makes no sense as to why this resolved the problem, but it does.

DropDownList event within Accordian and GridView

An event bound to a DropDownList in a standalone GridView obviously would work in this fashion , but things are bit more complicated in this scenario.
The event does not fire for the DropDownList. What's interesting is the event bound to the Button Does fire. Not sure what the difference would be between the DropDownList and TextBox.
I've tried both OnSelectedIndexChanged and OnTextChanged - neither work.
The nesting is as follows:
GridView A
Ajax Accordion
GridView B (With DropDownList)
<AjaxToolkit:AccordionPane ID="AccordionPane1" runat="server">
<Header>
</Header>
<Content>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false"
BorderWidth="0" AlternatingRowStyle-BorderStyle="None" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label runat="server" ID="lblType"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList runat="server" ID="ddlType" OnTextChanged="ddlType_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="TEST" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</Content>
Thank you!
UPDATE
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(object);
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(obj);

How to make a checkbox checked in a gridview using DataBinder.Eval

I am trying to make a checkbox checked if the value is 1 or 0 basically in my database i have
a field called Active (bit, not null) and i can pass the value to the gridview.. but now i am trying to make it checked if the bit is 1 or not checked if the bit is 0 but its not working.. it just shows unchecked but the bit is 1.
<ItemTemplate>
<asp:CheckBox ID="ItemCheck" runat="server"
Enabled='<%# (DataBinder.Eval(Container.DataItem, "Active")) %>' />
</ItemTemplate>
Any help would be much appreciated
Give this a shot:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Active"))%>' .. />
You can probably do it this way too:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#((bool)Eval("Active"))%>' .. />
You can use a CheckBoxField which will do this for you automatically and is a default subcontrol of a GridView
<asp:GridView ......>
<Columns>
<asp:CheckBoxField DataField="Active" SortExpression="Active" />
</Columns>
</asp:GridView>
This is all a matter of style but I prefer to use a RadioButtonList since it's usually more intuitive for a user
<asp:TemplateField ....>
<ItemTemplate>
<asp:RadioButtonList ID="rblActive" runat="server"
SelectedValue='<%# Bind("Active") %>'
RepeatDirection="Horizontal">
<asp:ListItem Value="1">Enabled</asp:ListItem>
<asp:ListItem Value="0">Disabled</asp:ListItem>
</asp:RadioButtonList>
<ItemTemplate>
</asp:TemplateField>

GridView edit command button with jquery

I want to use the gridview command button (edit) with the jquery not postback. Please help me.
Place an asp button in another item template and set its CommandName property to edit. This will work simillar to default edit button in grid view. Then you can call javascript function and perform your logic.
See the code below:
Remove the following line to avoid default edit button:
<asp:CommandField ShowEditButton="true" ShowCancelButton="true"/>
Add the following instead:
<asp:TemplateField HeaderText="headerName" >
<ItemTemplate>
<asp:Button ID="Button1" CommandName="edit" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
Hope this helps you..
I think you can do it in a gridview.What you need is to columns with textbox and display the data in the textbox and needs a button at the end .
<asp:TemplateField >
<HeaderTemplate>
Values
</HeaderTemplate>
<ItemTemplate>
<asp:textbox ID"txt" runat="server" cssclass="abc" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Spares">
<HeaderTemplate>
Edit column
</HeaderTemplate>
<ItemTemplate>
<asp:button id="abc" runat="server" text="save" cssclass="pqr" />
<input type="hidden" runat="server" value="" />
</ItemTemplate>
</asp:TemplateField>
Store the Id in a hidden field and it is possible to get the value of text box and hidden feild via jquery.
gridview will be rendered as html table and using parent() we can able to find the clicked row and after finding the row u can use find() to find the values in the txtbox and hidden feild. Use $ajax() or $post() to send data to server.

Categories

Resources