I am trying to make something like this:
Master detail gridview
But give me error in this line from cs file when i click Show details in the GridView:
gvRow.FindControl("GridView2").Visible = true;
I am trying when i click Show details to see the Names from other table where ID_Proba is = to ID from first Table.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
</Columns>
<EmptyDataTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = #id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
This is the cs file:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.FindControl("GridView2").Visible = true;
}
SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
GridView1.SelectedRow.FindControl("GridView2").Visible = true;
}
You need to take "GridView2" out of the EmptyDataTemplate and put it in a TemplateField. When you have data, the EmptyDataTemplate is not created at all, and also there is no grid in each row, because this template is for the whole grid.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = #id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
Related
I have a gridview control on page data is showing perfectly fine. I add edit button in row to update record its also working fine until I add another link in next column which redirect me to new page.
Here is my Gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataSourceID="SqlDataSource1" GridLines="None" DataKeyNames="issue_id,pro_id" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Key#" InsertVisible="False" SortExpression="issue_id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("issue_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view" CommandArgument ='<%# Eval("issue_id") %>' Text='<%# Eval("issue_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="pro_id" HeaderText="pro_id" SortExpression="pro_id" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="name" HeaderText="Project Name" SortExpression="name" />
<asp:BoundField DataField="type" HeaderText="Issue Type" SortExpression="type" />
<asp:BoundField DataField="summary" HeaderText="Summary" SortExpression="summary" />
<asp:BoundField DataField="mem_id" HeaderText="Member Name" SortExpression="mem_id" />
<asp:TemplateField HeaderText="Priority" SortExpression="priority">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Major</asp:ListItem>
<asp:ListItem>Critical</asp:ListItem>
<asp:ListItem>Minor</asp:ListItem>
<asp:ListItem>Cosmetic</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("priority") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
<asp:BoundField DataField="impact" HeaderText="Impact" SortExpression="impact" />
<asp:BoundField DataField="Expr1" HeaderText="Submit Date" ReadOnly="True" SortExpression="Expr1" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:IHDConnectionString %>"
SelectCommand="SELECT issue.issue_id, issue.pro_id, project.name, issue.type, issue.summary, issue.mem_id, issue.priority, issue.status, issue.impact, CONVERT (date, GETDATE()) AS Expr1 FROM issue INNER JOIN project ON issue.pro_id = project.pro_id"
UpdateCommand="UPDATE [issue] SET [mem_id] = #mem_id, [priority] = #priority, [status] = #status WHERE [issue_id] = #issue_id AND [pro_id] = #pro_id"
<UpdateParameters>
<asp:Parameter Name="mem_id" Type="String" />
<asp:Parameter Name="priority" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="issue_id" Type="Int32" />
<asp:Parameter Name="pro_id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
here is my redirect page control
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Redirect("~/redirect.aspx?view=" + e.CommandArgument);
}
Now problem is when I click edit it redirect me to another while it is suppose to edit the record in same page.
Change your Row_Command to this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.ToString() == "view")
{
Response.Redirect("~/redirect.aspx?view=" + e.CommandArgument);
}
}
I would recommend using a link when you need a link, you don't need to do postback to "redirect" the user:
<asp:TemplateField HeaderText="Key#" InsertVisible="False" SortExpression="issue_id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("issue_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<a href='redirect.aspx?view=<%# Eval("issue_id") %>'><%# Eval("issue_id") %></a>
</ItemTemplate>
</asp:TemplateField>
Error Message:
The variable name '#Bitnet' has already been declared. Variable names must be unique within a query batch or stored procedure. When trying to edit gridview.
I want to be able to update the checkbox field. This gridview is based on a table join.
<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="INST_ID,BITNET" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="INST_ID" HeaderText="INST_ID" ReadOnly="True" SortExpression="INST_ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Prefix" HeaderText="Prefix" SortExpression="Prefix" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:BoundField DataField="BITNET" HeaderText="BITNET" ReadOnly="True" SortExpression="BITNET" />
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked='<%# Eval("Active").ToString() == "1" ? true:false %>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked="true" Enabled="false" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
</asp:SqlDataSource>
You have to declare the parameter first. Then only you can use that. It should be something like this
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
<SelectParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</UpdateParameters>
</asp:SqlDataSource>
I solved my own question issue was that I was not clearing my parameters first
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Clear();
}
That fixed the issue it works fine now.
I am developing an ASP.NET intranet web application which is a Quiz Engine. Since I am a new ASP.NET developer, I have a table that shows some feedback received from the users and the admin has the ability to accept or reject these feedback using the DropDownList.
Under this table, there is a JQuery Accordion that when the Admin clicks on it, he will see a table with the received feedback for the last three months. But when the admin accepts or rejects one of the feedback and then clicks on the Accordion, he will see the that table but without that feedback even if the feedback is one of the feedbacks submitted last month. However, when you refresh the page and then clicks on the Accordion, he will see it.
So is there any functionality that will help me to get an auto-refresh table with refreshing the whole page?
Any help please?
ASP.NET Code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="950px" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"
DataTextField="Status" DataValueField="ID" AutoPostBack="true"
OnDataBound="DropDownList_DataBound" OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionShortcut
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
FilterExpression="[DivisionShortcut] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
<%--For the DropDownList--%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]">
</asp:SqlDataSource>
<%--Filtering by Division--%>
<asp:SqlDataSource ID="sqlDataSourceDivision" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [DivisionShortcut] FROM [Divisions]"></asp:SqlDataSource>
<dl id="jfaq">
<br />
<dt>Safety Suggestions List (for the last three months)</dt>
<dd>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
AllowSorting="True" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource4">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month"
SortExpression="SubmittedMonth" ReadOnly="True" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted)
AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title,
dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) < 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
</asp:SqlDataSource>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</dd>
</dl>
Code-Behind:
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
int suggestionStatus = int.Parse(ddl.SelectedValue);
GridViewRow row = (GridViewRow)ddl.NamingContainer;
string strID = GridView1.DataKeys[row.RowIndex]["ID"].ToString();
int ID = Int32.Parse(strID);
//For inserting the status in the database
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
string updateCommand = "UPDATE SafetySuggestionsLog SET [StatusID] = #StatusID WHERE [ID] = #ID";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#StatusID", suggestionStatus);
cmd.Parameters.AddWithValue("#ID", ID);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
UPDATE:
I used the UpdatePanel control to get a partial update, and I used <triggers>, but I got the following error and I don't know why:
Could not find an event named 'Click' on associated control
'GridView2' for the trigger in UpdatePanel 'UpdatePanel1'.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView2" EventName="Click" />
</Triggers>
<ContentTemplate>
<dl id="jfaq">
<br />
<dt>Safety Suggestions List (for the last three months)</dt>
<dd>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
AllowSorting="True" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource4">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month"
SortExpression="SubmittedMonth" ReadOnly="True" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted)
AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title,
dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) < 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
</asp:SqlDataSource>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</dd>
</dl>
</ContentTemplate>
</asp:UpdatePanel>
In the trigger you mentioned Click event, but in the code behind you have only DropDownList_SelectedIndexChanged event.
Instead you can trigger the SelectedIndexChanged event for the DropDownList as
<asp:AsyncPostBackTrigger ControlID="DropDownList" EventName="SelectedIndexChanged" />
You just need to trigger a valid event in the code behind.
Update: Since the SelectedIndexChanged updates the database, so you need to refresh the DataGrid to populate the updated data.
Add this line code at the end of SelectedIndexChanged method:
GridView2.DataBind();
use ajax update panel this may help http://ajax.net-tutorials.com/controls/updatepanel-control/
I need to show a Master/Child data in a page and I have used multiple GridViews to achieve the same. So, I have created two GridViews (Parent & Child) and now I want to fire the Button click event (i.e. btnLock) from the child gridview control and do some DB operations. So, I dont know how to achieve this.
Please help.
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:GridView Width="100%" AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False"
DataSourceID="sqlDsCustomers" runat="server" ShowHeader="False" OnRowCreated="gvCustomers_RowCreated">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="group" id='<%#String.Format("customer{0}",Container.DataItemIndex) %>' onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>,<%#String.Format("\"#order{0}\"",Container.DataItemIndex) %>)'>
<asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png"
Style="margin-right: 5px;" runat="server" /><span class="header">
<%#Eval("CustomerID")%>
:
<%#Eval("CompanyName")%>
(<%#Eval("TotalOrders")%>
Orders) </span>
</div>
<asp:SqlDataSource ID="sqlDsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT [OrderID], [OrderDate], [RequiredDate], [Freight], [ShippedDate] FROM [Orders] WHERE ([CustomerID] = #CustomerID)">
<SelectParameters>
<asp:Parameter Name="CustomerID" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
<div id='<%#String.Format("order{0}",Container.DataItemIndex) %>' class="order">
<asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
runat="server" ShowHeader="true" EnableViewState="false">
<RowStyle CssClass="row" />
<AlternatingRowStyle CssClass="altrow" />
<Columns>
<asp:TemplateField ItemStyle-CssClass="rownum">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
<asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="100px" />
<asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="110px" />
<asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="100px" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnLock" Text="Lock" CommandName="Lock" CommandArgument=<%# Eval("OrderID") %> runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Thanks
Use the RowCommand:
<asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
runat="server" ShowHeader="true" EnableViewState="false"
onrowcommand="gvOrders_RowCommand"
>
........
</asp:GridView >
protected void gvOrders_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Lock")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//dowork
}
}
I'm trying to code a Gridview that has a button on each row that when clicked will expose that particular rows data for use, but I'm not sure how the data would be passed.
The Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" Visible="False" />
<asp:BoundField DataField="RelationID" HeaderText="RelationID" InsertVisible="False"
SortExpression="RelationID" Visible="False" />
<asp:BoundField DataField="UserRole" HeaderText="UserRole" InsertVisible="False"
SortExpression="UserRole" Visible="False" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="Hash" HeaderText="Hash" InsertVisible="False" SortExpression="Hash"
Visible="False" />
<asp:BoundField DataField="DateCreated" HeaderText="Date Invited" SortExpression="DateCreated" />
<asp:TemplateField HeaderText="Resend Welcome Email">
<ItemTemplate>
<asp:Button runat="server" ID="btnResend" Text="Resend" OnClick="btnResend_Click" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
</asp:GridView>
The button_OnClick
protected void btnResend_Click(object sender, EventArgs e)
{
bool boolEmailSent = Email.sendWelcomeEmail(//Email from Row, //FirstName from Row, //Surname from Row, //Hash from Row);
if (boolEmailSent == true)
{
//Confirm to User
}
else
{
//TODO: write error to log
}
}
This article covers what you're attempting in more depth than we could answer here:
http://authors.aspalliance.com/aspxtreme/webforms/controls/addingbuttonfieldstoGridView.aspx
And here's another:
http://msdn.microsoft.com/en-us/library/bb907626.aspx