grid posts back when checkbox is checked - c#

I have this grid set up.... it all works totally fine... except one issue...
<asp:GridView runat="server"
ID="grdFacetsAssigned"
AllowPaging="false"
AllowSorting="True"
DataKeyNames="lngSystemFacet"
OnSelectedIndexChanging="grdFacetsAssigned_SelectedIndexChanging"
CssClass="table_scroll"
AutoGenerateColumns="False" GridLines="None"
ShowHeader="false" Width="500px"
OnSelectedIndexChanged="grdFacetsAssigned_SelectedIndexChanged"
ShowFooter="false" PagerSettings-Visible="false"
DataSourceID="SM_spStateUpdateReport_FacetAssignList"
OnRowCreated="grdFacetsAssigned_RowCreated">
<RowStyle CssClass="table_row" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbllngSystemFacetID" runat="server"
Text='<%# Eval("lngSystemFacetID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="strSystemSystemFacet" SortExpression="strSystemSystemFacet"
ItemStyle-Width="50%" />
<asp:TemplateField ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center"
SortExpression="bolAssigned">
<ItemTemplate>
<asp:CheckBox ID="chkFacetAssigned" runat="server"
OnClientClick="alert(this.checked);"
OnCheckedChanged="chkFacetAssigned_CheckedChanged"
AutoPostBack="True" Checked='<%# Eval("bolAssigned") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="intOrder"
HeaderText="Display Order" ItemStyle-Width="20%">
<ItemTemplate>
<asp:Label ID="lblAssignedFacetOrder" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "intOrder")%>'></asp:Label>
<asp:TextBox ID="txtAssignedFacetOrder" runat="server"
CssClass="gridview_input"
Text='<%#DataBinder.Eval(Container.DataItem, "intOrder")%>'
Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle CssClass="table_selected_row" />
<AlternatingRowStyle CssClass="table_alternating_row" />
<EmptyDataRowStyle CssClass="table_empty" />
<EmptyDataTemplate>
No Data
</EmptyDataTemplate>
</asp:GridView>
When you click the chkFacetAssigned checkbox the appropriate event fires. The code works well from there. What happens though is when the checkbox is checked... if the row is not selected there are two postbacks that happen. The first postback is from the grid and the second postback is from the checkbox. Both postbacks cause the chkFacetAssigned_CheckedChanged event to be called- resulting in code running twice that should only run once. I should note that if the row is already selected (the row the checkbox is on) you do not see this extra postback. Someone please help.
There are no other event handlers registered or anything like this.

First line of your code. Remove the following.
OnSelectedIndexChanging="grdFacetsAssigned_SelectedIndexChanging"
Second Line
OnSelectedIndexChanged="grdFacetsAssigned_SelectedIndexChanged"

What I did for this was a workaround in the checkbox event handler...
if (Page.Request.Params["__EVENTTARGET"].IndexOf("chkFacetAssigned") < 1)
{
return;
}
This ensures that the event is ignored unless it is responding to a postback that was initiated by the checkbox and not the grid.

Related

ASP.Net gridview editing - finding a control on a multipage grid view

I have a gridview that has a textbox in one of cells in my aspx file.
After clicking the edit link in the row, I am able to get the control programatically in the rowdatabound event with this:
e.Row.FindControl("controlPlaceholder");
Using the edit index from the edit event works as well.
However, the control is only enabled on the first page of the gridview. On any other page, FindControl() returns null.
What could be the reason for this? Are there any potential solutions to this issue? I've looked for a solution but haven't had any luck with finding someone who has this particular problem.
Markup:
<asp:Panel ID="pnlAccountAssignment" runat="server" CssClass="clsDataPanel" meta:resourcekey="pnlAccountAssignmentResource1">
<asp:GridView ID="gvAccountAssignment" runat="server" AutoGenerateColumns="False"
AllowPaging="true" PageSize="25" OnRowDataBound="gvAccountAssignment_RowDataBound"
OnRowCancelingEdit="gvAccountAssignment_RowCancelingEdit" OnRowEditing="gvAccountAssignment_RowEditing"
OnRowUpdating="gvAccountAssignment_RowUpdating" OnPageIndexChanging="gvAccountAssignment_PageIndexChanging"
OnSorting="gvAccountAssignment_Sorting" AllowSorting="True" meta:resourcekey="gvAccountAssignmentResource1">
<RowStyle BackColor="White" ForeColor="Black" />
<HeaderStyle CssClass="clsGrayBkgCell" HorizontalAlign="Center" VerticalAlign="Bottom" />
<Columns>
...Other Columns...
<asp:TemplateField meta:resourcekey="TemplateFieldResource8">
<ItemStyle CssClass="clsNumber" HorizontalAlign="Left" VerticalAlign="Top" />
<EditItemTemplate>
<asp:TextBox ID="column13PlaceHolder" runat="server" CssClass="clsNormalLabel">
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblprevMonth" runat="server" CssClass="clsNormalLabel"></asp:Label>
</ItemTemplate>
<HeaderTemplate>
<asp:LinkButton runat="server" ID="hypSortGLBal" OnClick="hypSortGLBal_Click"></asp:LinkButton>
</HeaderTemplate>
</asp:TemplateField>
...Other Columns...
</Columns>
<PagerStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
<asp:HiddenField ID="hdnPrepPopulated" runat="server" />
</asp:Panel>

How do I Pass Multiple Selected Datagrid Values to Another Page in asp.net?

I have multiple asp.net data grids that a user can select a checkbox to the corresponding value that they want, put in the amount they want in a textbox and submit the request. How do I show what values they requested with the amount on the next page? I can do this with a session I believe but I'm having a hard time finding good examples for something like this. Since they can select multiple values I can't use a query string right? I'm using VB.net but if you answer in c# that's fine. Thanks!
<asp:GridView ID="flexGridView" DataKeyNames="ID" runat="server" AutoGenerateColumns="False" DataSourceID="FormSqlDataSource" CssClass="gridView" ClientIDMode="Static">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Form" ShowHeader="False" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" ShowHeader="False" Visible="False" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty" >
<ItemTemplate>
<asp:TextBox ID="flexTextBox" runat="server" Width="40" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use Session or query string, but I would recommend Session, as there are limitations on the length of a query string, especially if you are not sure how many check boxes might be checked.
Use the OnCheckChanged event of the check box control and set AutoPostBack to true in your template field, like this:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server"
AutoPostBack="True" OnCheckedChanged="Check_Clicked" />
</ItemTemplate>
</asp:TemplateField>
protected void Check_Clicked(Object sender, EventArgs e)
{
// Store the check box name, ID or whatever unique value you want in Session here
CheckBox theCheckBox = sender as CheckBox;
// Was the check box found?
if(theCheckBox != null)
{
// Store in Session
Session["CheckBoxValue"] = theCheckBox.SomePropertyValue;
}
}
Then in the Page_Load of your redirect page, you will need to read out the Session value for the checked check boxes.

Conditional HyperLink in GridView?

I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).

When i Check the Checkbox how do I change that specific row style? C# ASP.NET

So i got this gridview on ASP.NET, it is about a gmail client, i made a template field with a check box that is for select several message at the same time, but first when you select a message i want to change the row background color, I already have my CSS but i dont know how to trigger the action or how to know what row was selected in the code. Here is the template of my grid:
<asp:GridView ID="inboxGrid" runat="server"
AutoGenerateColumns="false" CssClass="inbox" ShowHeader="false" BorderStyle="None" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<ItemStyle Width="24" CssClass="check"/>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="from" />
<ItemTemplate>
<asp:Label ID="lblFrom" runat="server"
Text = '<%# Eval("From") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="subject" />
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%# Eval("Subject") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="date" />
<ItemTemplate>
<asp:Label ID="lblFecha" runat="server" Text='<%# Eval("Fecha") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="inbox"/>
</asp:GridView>
Here is a solution using JQuery
Provided you add a CssClass named chk to asp:checkbox
<asp:CheckBox ID="chkSelect" runat="server" CssClass="chk" />
Here is the script
$(document).ready(function () {
$(".chk :checkbox").live("click", function () {
$(this).closest("tr").css("background-color", this.checked ? "#0000FF" : "");
});
});
Try this:
<asp:CheckBox ID="chkSelect" runat="server" OnClick="changeCss(this.id);" />
You can use toggleClass('CssClassName') to toggle the class on checkbox clicks:
function changeCss(ctrlId){
$(ctrlId).parent().parent().toggleClass('check');
}
The hierarchy is tr > td > checkbox, we go reverse to change the css of tr/row.

Edit and update gridview rows on mouse click event

I want to edit and update gridview rows on mouse click event. If the user clicks on a row it should become editable and when he again clicks on the same row it should become uneditable and the changes should be saved.
my gridview looks like:-
<asp:GridView ID="grdexcelsheet" runat="server" AllowPaging="True"
AutoGenerateColumns="False" RowStyle-HorizontalAlign="Left" HorizontalAlign="Center"
onpageindexchanging="grdexcelsheet_PageIndexChanging"
Width = "100%" Height = "100%" Font-Size="Small">
<PagerSettings Position="Bottom" Mode="NextPreviousFirstLast" FirstPageText="First"
LastPageText="Last" NextPageText="Next" PreviousPageText="Prev" PageButtonCount="5" />
<Columns>
<asp:BoundField DataField="SrNo" HeaderText="SrNo" ReadOnly="true" />
<asp:TemplateField HeaderText="CustomerFieldName">
<ItemTemplate>
<asp:Label ID="Label4" Runat="server" Text='<%# Bind("CustomerFieldName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerFieldName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyFieldName" HeaderText="CompanyFieldName" ReadOnly="true" />
<asp:BoundField DataField="Usage" HeaderText="Usage" ReadOnly="true" />
</Columns>
</asp:GridView>
There are many ways to do this. You can use AJAX Control Toolkit Modal Popup Extender for updating your grid. Also, you can make use Update Panel from the same toolkit.
If you are willing to use third party controls...please use this which will be very to use
http://obout.com/grid/grid_commands.aspx...

Categories