Get clicked button from Gridview onInit - c#

I have a gridview (default) with select buttons. Is it possible to get on which button was clicked in event onInit() ?
I tried to use
Request["__EVENTTARGET"];
but this returns only "GridView1". Thanks you for the answers
EDIT
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="gridView" DataKeyNames="Nazev" DataSourceID="OblastiSource" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Nazev" HeaderText="Nazev" ReadOnly="True" SortExpression="Nazev" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" ButtonType="Button" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
You should know, that DataField="Nazev" is the number I spoke about. In reality there is a M:N relation behind (I wanted to keep it simple for you).
I have two tables: "Towns" & " Streets". The gridview1 shows the Towns names. When the user select a town, I want to generate checkboxes with "street names". First the checked and add to Panel1 and then the unchecked and add to Panel2. Checked means, that the street is in the town.
So the town have lot of streets and one street have lot of towns (M:N relation). Do you have some smoother solution? I want to keep this structure.

Related

How to make any column hyperlinked in dynamic gridview , where that hyperlink column is linked to another gridview

How to make any column hyperlinked (except first one) in dynamic gridview , where that hyperlink column is linked to another gridview.
This dynamic table shows two type of output as mentioned below:
View 1:
Start Bus_no Change_at Change_Bus_no Destination Means
Desu Office 715 Hanuman Mandir 781 Subroto Park DTC
Desu Office 715 Palam Airport 764 Subroto Park DTC
Desu Office 715 Palam Airport 781 Subroto Park DTC
View 2:
Start bus_no Destination Means
Subroto Park 764 Nehru Place Terminal DTC
In first view I want to make column 2nd (Bus_no) and 4th(Changed_Bus_no) and in second view I want to make column 2nd as hyperlink column which will be linked to another grid view table.
Gridview Code is given below:
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
please help. Thanx in advance.
You can use Template Field for your requirement.
Try like this,
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Vertical" ForeColor="Black">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
<Columns>
<asp:TemplateField HeaderText="First Column">
<ItemTemplate>
<asp:Label ID="lblthird0" runat="server" Text="items" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Second Column">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#Gridview2"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can do like this for all your columns.

Identify the Access Key of a particular record in Gridview ,(For a button / TemplateField)

I am trying to have a gridview with dynamic data imported using a SqlDataSource , plus 2 TemplateFields containing 2 buttons as Accept and Decline.What I need to do is , update a specific column of the table where the records are coming from.(Change the value from Null to 1 or 0, in the column called "accepted")
Q.)I need a way to identify the row which the button will be clicked ?
(need to identify the value of access key for the particular row)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="request_no" DataSourceID="SqlDataSource1" Font-Names="Tahoma" Font-Size="Small" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="request_no" HeaderText="request_no" ReadOnly="True" SortExpression="request_no" />
<asp:BoundField DataField="employee_name" HeaderText="employee_name" SortExpression="employee_name" />
<asp:BoundField DataField="requested_date" HeaderText="requested_date" SortExpression="requested_date" />
<asp:BoundField DataField="requested_time" HeaderText="requested_time" SortExpression="requested_time" />
<asp:BoundField DataField="requested_from" HeaderText="requested_from" SortExpression="requested_from" />
<asp:BoundField DataField="requested_to" HeaderText="requested_to" SortExpression="requested_to" />
<asp:BoundField DataField="service_type" HeaderText="service_type" SortExpression="service_type" />
<asp:BoundField DataField="reason_data" HeaderText="reason_data" SortExpression="reason_data" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AcceptRequest" runat="server" OnClick="AcceptRequest_Click" Text="Accept" CommandName="AcceptRequestcmd" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="DeclineRequest" runat="server" OnClick="DeclineRequest_Click" Text="Decline" CommandName="DeclineRequestcmd" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
You are very close, just lack CommandArgument:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AcceptRequest" runat="server"
OnClick="AcceptRequest_Click" Text="Accept"
CommandName="AcceptRequestcmd"
CommandArgument="<%# Eval("request_no") %> />
</ItemTemplate>
</asp:TemplateField>
(assuming that request_no is the row identifier)
Then just
protected void GridView1_RowCommand(...)
{
switch ( e.CommandName )
{
case "AcceptRequestcmd" :
string rowid = e.CommandArgument;
}
}

How to make gridview searchable?

I've got a gridview that draws from multiple tables, I'd like to make it searchable in three fields, one from each table. I plan on using a dropdown to select which field to search for, and a textbox of course for search input, but what then? Here's my gridview code in C# ASP.NET:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." AllowPaging="True"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" HorizontalAlign="Center"
DataKeyNames="SubId" AllowSorting="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="SubId" HeaderText="Submission ID"
SortExpression="SubId" >
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="CustName" HeaderText="Customer"
SortExpression="CustName" />
<asp:BoundField DataField="CustCity" HeaderText="Customer City"
SortExpression="CustCity" />
<asp:BoundField DataField="CustState" HeaderText="Customer State"
SortExpression="CustState" />
<asp:BoundField DataField="BroName" HeaderText="Broker "
SortExpression="BroName" />
<asp:BoundField DataField="BroState" HeaderText="Broker State"
SortExpression="BroState" />
<asp:BoundField DataField="EntityType" HeaderText="EntityType"
SortExpression="EntityType" />
<asp:BoundField DataField="Coverage" DataFormatString="{0:c}"
HeaderText="Coverage" SortExpression="Coverage" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="View.aspx?SubId={0}" Text="View" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="ViewEdit.aspx?SubId={0}" Text="Edit" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
ProviderName="<%$ ConnectionStrings:ProductInstanceString.ProviderName %>"
SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Submission.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId">
</asp:SqlDataSource>
Use dropdown on the page and on the codebehind use DataView object to filter rows of grid, see below syntax:
DataView dvData = new DataView(dtData);
dvData.RowFilter = "[ColumnName]=[Condition, this will be your dropdown value]";
gv.DataSource = dvData;
gv.DataBind();

Button in gridview that redirects user to url

I'm working on a manager of sorts that allows certain employees to view the results of a survey customers take. I'm tracking the IP address of the customers that take the survey and displaying that ip address to the employees who can view the results. I'm using a gridview with an sqldatasource to extract the data out of the database and display it.
What I would like to do is add a button or link that takes the IP address and appends it to a url of web site to track the location of the ip address on a map. I have already signed up for a serivce to do this but am unsure of how to code it.
Here is my Gridview
<asp:GridView ID="GVnewsletterManager" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="email_time" ForeColor="Black"
GridLines="Horizontal" Width="100%" PageSize="15">
<Columns>
<asp:BoundField DataField="email_time" HeaderText="Length of Email Subscription"
SortExpression="email_time" />
<asp:BoundField DataField="reason" HeaderText="Reason for Unsubscribe"
SortExpression="reason" />
<asp:BoundField DataField="other" HeaderText="Other Reason"
SortExpression="other" />
<asp:BoundField DataField="other_comments" HeaderText="Other Comments" SortExpression="other_comments"
ReadOnly="True" />
<asp:BoundField DataField="DateTime" HeaderText="Date"
SortExpression="DateTime" />
<asp:BoundField DataField="IP" HeaderText="Client IP" SortExpression="IP" />
<asp:ButtonField ButtonType="Button" Text="Trace IP to Map" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"
HorizontalAlign="Left" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
Thank you in advance for the help!
Try using a HyperLinkField
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.aspx
<asp:HyperLinkField
HeaderText="Client IP"
DataNavigateUrlFields="IP"
DataNavigateUrlFormatString="some-page.aspx?id={0}"
DataTextField="IP"
DataTextFormatString="{0}"
Target="_blank"/>

How does FindControl work in GridView?

I am trying to update a database using the GridView edit, update CommandField. I have two editable fields which are displayed as text boxes when in edit mode. When clicking submit, I am trying to put the text box values into variables to work with, but I am unable to access them. The two column names are "EOR" and "CategoryName". I have found several suggestions on other forums to try something like:
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEor = (TextBox)myGridName.Rows[e.RowIndex].FindControl("EOR");
When I debug the program, txtEor is always null. The only thing i can think of is that I am not referencing the cell properly. I set the Headertext, AccessibleHeaderText, DataField, and SortExpression to "EOR" but it still just comes up null.
Any help would be greatly appreciated!
asp for the gridview:
<asp:GridView ID="grdEOR" runat="server" BackColor="White"
BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
AllowPaging="True"
PageSize="15" AutoGenerateColumns="False" onrowediting="grdEOR_RowEditing"
onrowcancelingedit="grdEOR_RowCancelingEdit"
onrowupdating="grdEOR_RowUpdating" onrowdeleting="grdEOR_RowDeleting"
ShowFooter="True">
<PagerSettings Mode="NumericFirstLast" />
<Columns>
<asp:BoundField DataField="EORCategoryID" HeaderText="EORCategoryID"
SortExpression="EORCategoryID" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="EOR" HeaderText="EOR" SortExpression="EOR"
AccessibleHeaderText="EOR"/>
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True"
ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black"
BorderStyle="Solid" BorderWidth="5px" />
</asp:GridView>
I finally found a way that works:
string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

Categories

Resources