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

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?

Related

EditItemTemplate and InsertItemTemplate wont allow dropdownlist designer entry

I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!
<asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
<EditItemTemplate>
<asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
<asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

HtmlInputCheckbox onselect event not getting invoked

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>

Eval() in a DropDownList within a GridView

How can I do Eval() in a DropDownList that are inside a GridView?
I tried the follow code, but still wrong:
<asp:TemplateField HeaderText="Prioridade">
<ItemTemplate>
<asp:DropDownList ID="ddlPrioridade" runat="server"
SelectedValue='<%# Eval("Prioridade") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Try this way
<asp:DropDownList ID="ddlPrioridade" runat="server" AppendDataBoundItems="true" DataSourceID="datasource" DataTextField="key" DataValueField="data" AutoPostBack="True" SelectedValue='<%# Eval("Prioridade") %>'>
also have a look at this DropDownList inside a GridView (or DataGrid). It might help you
You are receiving this error because the drop down list is not loaded with items and asp.net is trying to set the selected value of an empty drop down list and that's why it gives you error. So fill the drop down list first before setting it's selected value.

GridView within an UpdatePanel - Get Updated Textbox values on button click

IGNORE THIS:
I was rebinding the grid in the Onload method.
I have a grid view with some textboxes, which I have to wrap in a UpdatePanel Control. So it looks like this:
<asp:UpdatePanel ID="upDistribution" runat="server" OnLoad="upDistribution_OnLoad">
<ContentTemplate>
<asp:GridView ID="gvDistributions" runat="server" AutoGenerateColumns="false"
OnRowDataBound="gvDistributions_RowDataBound"
CssClass="TallCells ContrastTable MaxWidth LeftHeaders"
GridLines="Both" ShowFooter="True" style="">
<RowStyle HorizontalAlign="Left" />
<EmptyDataTemplate>
No pricing history data found.
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="PriceA">
<ItemTemplate>
<asp:TextBox ID="txtPriceA" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DistPrice">
<ItemTemplate>
<asp:TextBox ID="txtDistPrice" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
There is a button "Save" when I would like to get the value entered for the txtboxes. If there is no UpdatePanel around the GV, i can can easily get the txtbox values like:
((TextBox)gvDistributions.Rows[0].Cells[0].FindControl("txtPriceA")).Text
But when, the GridView is wrapped with UpdatePanel, the above statement returns the value that was set on the page load.
How can I get the value of the text box without getting rid of the updatePanel. I need the update panel, because the number of rows, and dates are dependent on another variable in another user control.
As per my understanding, you should use a trigger for the update panel which is something like below.
<asp:UpdatePanel ...>
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Hope this helps!!
Use your TextBoxes with their AutoPostBack property set True
<asp:TextBox ID="txtPriceA" runat="server" AutoPostBack="True"/>
I was rebinding gridview in the onLoadMethod

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