How to find which page in DetailsView holds selected GridView row - c#

I am setting up HealthMonitoring and I am writing a couple of pages to manage the event logs.
I have one page which has a GridView and another that has a DetailsView. The GridView has a template button on each row with an onClick event that loads the second page with the DetailsView on it.
What I would like to do is; when I click the template button on the GridView it then loads the second page with the DetailsView and inserts that particular record from the GridView into the DetailsView table.
The DetailsView must have paging enabled. I am stuck trying to find out which page index to load with the right record from the GridView. Currently it will just load up the first page index, then I have to click next to the record I need.
Without the paging enabled, I can read my 2 global variables and then load the correct record into the DetailsView table, but with paging enabled I'm not sure how to do it.
On the GridView page:
protected void Details1_ButtonClick(object sender, EventArgs e)
{
//I set 2 global variables here of the selected EventId and
//Details to read when the next page loads
Response.Redirect("ErrorDetails.aspx");
}
On the DetailsView page.
protected void Page_Load(object sender, EventArgs e)
{
//without paging I can set the SqlDataSource1.SelectCommand to select
//the correct record using one of the global variables
}
I have tried to use DetailsView1_PageIndexChanged, DetailsView1_Load and Page_Load to get the value in the first row, but for some reason it is allways a page behind. The variable always shows the ID of the previous page when it loads. I was going to try and keep track of the page for deleting records, but it will not work either.
This is the DataSource and DetailsView in ErrorDetails.aspx:
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource1" DataKeyNames="EventId" AutoGenerateRows="False" OnItemDeleted="DetailsView1_ItemDeleted" OnItemDeleting="DetailsView1_ItemDeleting" AllowPaging="True" OnLoad="DetailsView1_Load" OnPageIndexChanged="DetailsView1_PageIndexChanged" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<AlternatingRowStyle CssClass="alt" />
<Fields>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="EventId" HeaderText="EventId" />
<asp:BoundField DataField="EventTimeUtc" HeaderText="EventTimeUtc" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" />
<asp:BoundField DataField="EventType" HeaderText="EventType" />
<asp:BoundField DataField="EventSequence" HeaderText="EventSequence" />
<asp:BoundField DataField="EventOccurrence" HeaderText="EventOccurrence" />
<asp:BoundField DataField="EventCode" HeaderText="EventCode" />
<asp:BoundField DataField="EventDetailCode" HeaderText="EventDetailCode" />
<asp:BoundField DataField="Message" HeaderText="Message" />
<asp:BoundField DataField="ApplicationPath" HeaderText="ApplicationPath" />
<asp:BoundField DataField="ApplicationVirtualPath" HeaderText="ApplicationVirtualPath" />
<asp:BoundField DataField="MachineName" HeaderText="MachineName" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" />
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:Label ID="Label1" runat="server">
<%= EventVariables.EventDetails %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Fields>
<PagerSettings Mode="NextPreviousFirstLast" />
<PagerStyle CssClass="pager" />
</asp:DetailsView>
This is the DataSource and GirdView from the parent form:
<asp:GridView
ID="ErrorGrid"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
DataKeyNames="EventId"
OnRowDeleting="ErrorGrid_RowDeleting"
AllowPaging="True"
AllowSorting="True"
Font-Size="Small"
CellPadding="10"
CellSpacing="1" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Details1" Text="Details" runat="server" AutoPostBack="true" OnClick="Details1_ButtonClick" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" />
<asp:BoundField DataField="EventTime" HeaderText="EventTime" SortExpression="EventTime" />
<asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" SortExpression="RequestUrl" />
<asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" SortExpression="ExceptionType" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT * FROM [aspnet_WebEvent_Events]"
DeleteCommand="DELETE FROM [aspnet_WebEvent_Events] WHERE [EventId]=#EventId">
<DeleteParameters>
<asp:Parameter Name="EventId" />
</DeleteParameters>
</asp:SqlDataSource>

I managed to solve this by doing a calculation. As there is 10 rows per page in the GridView I'm able to calculate the page by doing this:
EventVariables.EventPage = ((((ErrorGrid.PageIndex + 1) * 10) - 10) + Row.RowIndex);
And then loading the page in the DetailsView from the variable when it loads.

Related

want to update the column of specific row of gridview

I have added a buttonfield named "accept" in the gridview..I want to update the column 'status' of bitfield to '1' of that specific row when admin clicks on accept.. Im working on c#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server" Width="1000px"
AllowPaging="True" PageSize="8"
CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" DataSourceID="SqlDataSource1" AllowSorting="True">
<Columns>
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>
<asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
<asp:BoundField DataField="emailid" HeaderText="emailid" SortExpression="emailid" />
<asp:BoundField DataField="contact" HeaderText="contact" SortExpression="contact" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbrubyConnectionString3 %>" SelectCommand="SELECT seller.fname, seller.emailid, seller.contact,seller.type, login.username, login.password FROM seller INNER JOIN login ON seller.sid=login.lid where login.status= ORDER BY login.createdate ">
Add an event handler for GridView1_RowCommand, looking for the command "Update", and grab the DataItem so you know which item to update, then update it in the database, and then rebind your gridview.
You could also make an ajax call to update the value, which would be faster (less data to transmit and receive), and wouldn't require a full page refresh, but that is more work to set up.
Add an identifier (CommandArgument) to your command by changing:
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"/>
to:
<asp:ButtonField Text="Accept" CommandName="Update" runat="server"
CommandArgument="<%# Eval("emailid") %>"/>
Add event handler by changing:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
DataSourceID="SqlDataSource1" AllowSorting="True">
to:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" runat="server"
Width="1000px" AllowPaging="True" PageSize="8" CssClass="Grid"
AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
DataSourceID="SqlDataSource1" AllowSorting="True"
OnRowCommand="GridView1_OnRowCommand">
Then write your update in the backend by adding:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch(e.CommandName)
{
case "Accept":
var email = e.CommandArgument.ToString();
Accept(email); // write this function
break;
}
}

Selection of checkboxes lost during pagination of gridview

I have a problem in gridview, as per requirement i have set No of Records per page = 4 in gridview. I have to select Checkbox against every complaint but problem is then when i got to next pge in gridview and e.g fro 1 to 2 then when i come back to page 1 then it doesn't show TICK in check boxes . It doesn't remember my selection when i browse to and back to page.
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333" Width="550px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" >
<ItemStyle Wrap="True" />
</asp:BoundField>
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
please check the above link.here your problem was clearly explained here.i think so it may be helpfull
As per comments...
If you do not update the underlying database by processing the OnCheckChanged event of the check box, then it will simply be reading the same data all the time.
From How to add event for Checkbox click in Asp.net Gridview Column, I have extracted the required information and tried to modify to fit your initial question.
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true/>
</ItemTemplate>
</asp:TemplateField>
add checkbox change event in aspx.cs page
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
<your data source>.Rows[row.DataItemIndex]["B"] = ((CheckBox)GridViewSmsComplaints.Rows[row.RowIndex].FindControl("ckboxIsComplaint")).Checked;
}

How to make a hyperlink through GridView column value?

I want here that user can see the answer under the question by selecting its heading. The question should be a hyperlink to redirect me on the page Answer.aspx. I mean to say that when user take cursor over the How to do this? it should redirect the user to the desired page. How can I do that?
here is the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="100%" BorderStyle="None">
<Columns>
<asp:BoundField DataField="QuestionHEAD" HeaderText="Question"
SortExpression="QuestionHEAD" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Problem" HeaderText="Problem"
SortExpression="Problem" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
<asp:BoundField DataField="Forum" HeaderText="Forum" SortExpression="Forum" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Username" HeaderText="Asked By"
SortExpression="Username" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="SELECT [QuestionHEAD], [Problem], [Forum], [Username] FROM [Question]">
</asp:SqlDataSource>
You can use a HyperLinkField for this:
<asp:HyperLinkField DataNavigateUrlFormatString="/somepage.aspx?a={0}&b={1}" DataNavigateUrlFields="Column1, Column2" />
Try something like this
Eval("id") should be the question id and Eval("Question") should be the question field in your database
<asp:TemplateField HeaderText="Question" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Eval("Question")%>
</ItemTemplate>
</asp:TemplateField>

Gridview page index updating only when it feels like it

I have a gridview that receives a data source with 8621 entries with the page size set to 20. The first page is displayed exactly as it should be. I click the link to display page 2, and it also displays as it should. Anything after that however is still page 2. So if I click the link to go to page 3, the paging control updates saying I am on page 3, but the data displayed is the data on page 2. I know the gridview is getting all 8621 entries because if I change the page size to 200 it displays those 200. Why would the gridview work correctly for some pages but not others? Anyways, enough with my rambling, here is the code itself:
<ajax:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<cb:SortedGridView ID="SearchUsersGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="Email"
DefaultSortDirection="Ascending" DefaultSortExpression="Email"
AllowPaging="true" PageSize="20" AllowSorting="true" Width="100%" SkinID="PagedList">
<Columns>
<asp:TemplateField HeaderText="In List">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:CheckBox ID="IsInEmailList2" runat="server" checked='<%#IsInEmailList(Container.DataItem)%>' OnCheckedChanged="IsInEmailList_CheckedChanged" AutoPostBack="true" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:HyperLink ID="NameLink2" runat="server" Text='<%# Eval("Email") %>' NavigateUrl='<%#GetEditUserUrl(Container.DataItem)%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="LastName">
<HeaderStyle horizontalalign="Left" />
<ItemStyle horizontalalign="Left" />
<ItemTemplate>
<asp:Label ID="FullNameLabel2" runat="server" Text='<%#GetFullName(Container.DataItem)%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div align="center">
<asp:Label runat="server" ID="noUsersFound" enableViewState="false" Text="No matching users found."/>
</div>
</EmptyDataTemplate>
</cb:SortedGridView>
</ajax:UpdatePanel>
and some relevant code behind:
SearchUsersGrid.Visible = true;
SearchUsersGrid.DataSourceID = "SearchUsersDs";
SearchUsersGrid.DataBind();
and the data source itself
<asp:ObjectDataSource ID="SearchUsersDs" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="FindUsersByName" TypeName="Builder.Users.UserDataSource"
SelectCountMethod="CountUsersByName" EnablePaging="True" SortParameterName="sortExpression" DataObjectTypeName="Builder.Users.User" DeleteMethod="Delete">
<SelectParameters>
<asp:ControlParameter ControlID="SearchText" Name="searchPattern" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="SearchByDropDown" Name="searchField" PropertyName="SelectedValue" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
There is no PageIndexChanged event. Any ideas?
Remove the AJAX update panel and check if it works as expected.. Another problem might be that if you bind the Grid in page_load make sure it is not posted back everytime.. put it in
if(!IsPostBack)
{
SearchUsersGrid.Visible = true;
SearchUsersGrid.DataSourceID = "SearchUsersDs";
SearchUsersGrid.DataBind();
}
I think the issue had to do with our production server. for some reason the code works great on our production box (even with the same data).

Is there a way to make asp.net Boundfield a link?

Im sorry if this has been asked before, but i have searched the forums for quite some time and have not been able to find a solution to my problem.
I am currently developing a web application where users are able to donate bottle deposit. When users have uploaded the bottles they will be shown in the following table:
Snippet of the table
The data is shown in a gridview with a SqlDataSource. The code:
<div class="table-responsive col-8">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [creator], [amount], [details],
[imageurl], [pickup], [available_from] FROM [deposit] ORDER BY [id] DESC"></asp:SqlDataSource>
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
I would like to be able to click on each individual post and create dynamic pages where more data from the database is shown. I read somewhere that i was possible to create these page with the ID of each post but i couldnt find anything that showed how it works.
Be free to ask any follow questions. This is my first post so im not sure if i have covered everything.
Thanks a lot in advance.
One way to do this is with a link in each row. First, you need to add "id" to the SELECT in your DataSource, so the data is accessible at the time the table is bound.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WeCANConnectionString %>"
SelectCommand="SELECT [id], [creator], [amount], [details], [imageurl],
[pickup], [available_from] FROM [deposit] ORDER BY [id] DESC">
</asp:SqlDataSource>
Then in your GridView, make a template column with a HyperLink, and have the NavigateUrl of that HyperLink open a new page with the ID on the query string. If you don't want the ID to be exposed in the URL, you may need to obfuscate it in some way, or use perhaps a GUID column in the DataBase for each record. As long as you can identify it. Your NavigateUrl can be whichever page you want, but in this example I used DetailPage.aspx.
<asp:GridView id="GridView1" class="table table-hover table-striped table-bordered" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="DetailsLink" runat="server"
NavigateUrl="DetailPage.aspx?id=<%# Eval("id")%>"
Text="Details" Target="_blank">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="creator" HeaderText="Uploaded By" SortExpression="creator" />
<asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
<asp:BoundField DataField="details" HeaderText="Details" SortExpression="details" />
<asp:BoundField DataField="imageurl" HeaderText="Image" SortExpression="imageurl" />
<asp:BoundField DataField="pickup" HeaderText="Location of the bottles" SortExpression="pickup" />
<asp:BoundField DataField="available_from" HeaderText="Available from" SortExpression="available_from" />
</Columns>
</asp:GridView>
The <## Eval('id')#> will render the ID for each row as a query string in the URL.
If I understand you well, You can use Template Field
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>

Categories

Resources