Gridview not updating value - c#

I am trying to make my gridview update a SQL record but it is not updating the values.
Here is the aspx code:
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
onrowcancelingedit="GridView1_RowCancelling"
OnRowDeleting ="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
DataKeyNames="RID"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:TemplateField Visible="false" HeaderText="RID">
<ItemTemplate>
<asp:Label runat="server" ID="RID" Text='<%#Bind("RID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Description">
<ItemTemplate>
<asp:Label runat="server" ID="short_lbl" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="SHORT_DESCRIPTION" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valShort" ControlToValidate="SHORT_DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server" ID="desc_label" Text='<%#Bind("DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="DESCRIPTION" Text='<%#Bind("DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valLast" ControlToValidate="DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the update function itself.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label RID = GridView1.Rows[e.RowIndex].FindControl("RID") as Label;
TextBox SHORT_DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("SHORT_DESCRIPTION") as TextBox;
TextBox DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("DESCRIPTION") as TextBox;
String UpdateQuery = string.Format("UPDATE TAXONOMIES SET SHORT_DESCRIPTION='{0}', DESCRIPTION='{1}' WHERE RID = {2}", SHORT_DESCRIPTION.Text, DESCRIPTION.Text, Convert.ToInt32(RID.Text));
GridView1.EditIndex = -1;
BindGridData(UpdateQuery);
}
If I put a breakpoint and look at the value, I can see that is is not the newly typed in value. I'm not sure what is going on.

I think the problem is you are binding you grid on the page-load event.
try binding it in if (!IsPostBack)
Example
private void Page_Load()
{
if (!IsPostBack)
{
//bind your grid here.
}
}

use this code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="regGroupID" HeaderText="regGroupID"
SortExpression="regGroupID" />
<asp:BoundField DataField="amountReceived" HeaderText="amountReceived"
SortExpression="amountReceived" />
<asp:BoundField DataField="other" HeaderText="other" SortExpression="other" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Table]"
UpdateCommand="UPDATE [Table] SET [regGroupID] = #regGroupID, [amountReceived] = #amountReceived, [other] = #other WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
<asp:Parameter Name="other" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
here I set id as the primary key
table structure is

Try changing the Eval to Bind. Eval is read-only so you can't actually update the value.

Related

How to take ID of a row in gridview when you press a button in a row

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="IDProject" DataSourceID="SqlDataSource1" GridLines="Vertical" style="margin-top:2rem" Width="1056px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField SortExpression="ProjectLeader" HeaderText="Project Leader">
<ItemTemplate>
<asp:Label Text='<%# Bind("ProjectLeader") %>' ID="lvProjectLeader" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" SortExpression="ProjectName" />
<asp:TemplateField HeaderText="Start date:" SortExpression="StartDate">
<ItemTemplate>
<asp:Label Text='<%# Bind("StartDate") %>' ID="lbStartDate" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name" SortExpression="Client name">
<ItemTemplate>
<asp:Label Text='<%# Bind("Client name") %>' ID="lbClientName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employees">
<ItemTemplate>
<asp:Button Text="Open" ID="EmployeesOpen" OnClick="EmployeesOpen_Click" runat="server" CssClass="btn btn-outline-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="Insert into Project values(#ProjectName, #ClientID,#ProjectLeader,#StartDate)"
SelectCommand="select IDProject, ProjectLeader, ProjectName, StartDate, ClientID, Client.Name from Project inner join Client on IDClient = ClientID"
UpdateCommand="Update Project set ProjectName = #ProjectName where IDProject = #IDProject">
<InsertParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="ClientID" />
<asp:Parameter Name="ProjectLeader" />
<asp:Parameter Name="StartDate" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="IDProject" />
</UpdateParameters>
</asp:SqlDataSource>
I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button inside that column that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
public partial class Project : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EmployeesOpen_Click(object sender, EventArgs e)
{
Response.Redirect("Employees_On_Project.aspx");
}
}
You will find what you are looking for here : https://forums.asp.net/t/1957409.aspx?Gridview+get+SelectedRow+in+button+click+event

How can i add dropdown list with files name in footer section?

My problem is the next:
I have to make a page which must contain test questions.
I already made it for normal questions, but in the next point, i need a dropdown list in the footer template where the user can choose images names from this list and the textbox get the file path.
The files path is the following:
"~/Dokuments/" + v_tren_dirnev(different folder for every test) + "/Teszt/"
The code for aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Kerdesek_Valaszok.aspx.cs" Inherits="Kerdesek_Valaszok" %>
<%# Register Src="UserControl/AutoRedirect.ascx" TagName="AutoRedirect" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2"
runat="Server">
<uc1:AutoRedirect ID="AutoRedirect1" runat="server" />
<asp:SqlDataSource ID="SqlDSTreningek" runat="server" ConnectionString="<%$ ConnectionStrings:dbcs %>"SelectCommand="SELECT * FROM [trening] WHERE ([tren_deleted] = #tren_deleted)">
<SelectParameters>
<asp:Parameter DefaultValue="false" Name="tren_deleted" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ObjectDataSource ID="odsKerdesek" runat="server" DeleteMethod="DeleteKerdes" InsertMethod="InsertKerdes" SelectMethod="GetAllKerdes" TypeName="trening.KerdesekDataAccessLayer" UpdateMethod="UpdateKerdes">
<SelectParameters>
<asp:Parameter Name="TrenId" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="KerdId" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="KerdTrenId" Type="Int32" />
<asp:Parameter Name="KerdKerdes" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="KerdId" Type="Int32" />
<asp:Parameter Name="KerdKerdes" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="odsValaszok" runat="server" DeleteMethod="DeleteValasz" InsertMethod="InsertValasz" SelectMethod="GetAllValasz" TypeName="trening.ValaszokDataAccessLayer" UpdateMethod="UpdateValasz">
<SelectParameters>
<asp:Parameter Name="KerdId" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ValaId" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ValaKerdId" Type="Int32" />
<asp:Parameter Name="ValaValasz" Type="String" />
<asp:Parameter Name="ValaHelyes" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ValaId" Type="Int32" />
<asp:Parameter Name="ValaValasz" Type="String" />
<asp:Parameter Name="ValaHelyes" Type="Boolean" />
</UpdateParameters>
</asp:ObjectDataSource>
<br />
<table id="kerdesek_valaszok">
<tr>
<td colspan="3">
<h2>Tréning kérdések és válaszok karbantartása</h2>
</td>
</tr>
<tr>
<td colspan="3">
<asp:DropDownList ID="ddlTreningek" runat="server" DataSourceID="SqlDSTreningek" DataTextField="tren_megnevezes" DataValueField="tren_id" Font-Size="Large" AutoPostBack="True" OnDataBound="ddlTreningek_DataBound" OnSelectedIndexChanged="ddlTreningek_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="gvKerdesek" runat="server" AutoGenerateColumns="False" DataKeyNames="kerd_id" DataSourceID="odsKerdesek" CellPadding="4" ShowFooter="True" ForeColor="#333333" GridLines="None" HorizontalAlign="Center" OnRowCommand="gvKerdesek_RowCommand" OnSelectedIndexChanged="gvKerdesek_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="kerd_id" InsertVisible="False" SortExpression="kerd_id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("kerd_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("kerd_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="kerd_tren_id" InsertVisible="False" SortExpression="kerd_tren_id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("kerd_tren_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("kerd_tren_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kérdések" SortExpression="kerd_kerdes">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("kerd_kerdes") %>' MaxLength="250" Size="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTextBox1" runat="server" ErrorMessage="Kötelező a kérdést megadni!" ValidationGroup="update" ControlToValidate="TextBox1" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("kerd_kerdes") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txbNewKerdes" runat="server" MaxLength="250" Size="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTextBox2" runat="server" ErrorMessage="Kötelező az új kérdést megadni" ValidationGroup="insert" ControlToValidate="txbNewKerdes" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="btnUpdateKerdes" runat="server" CausesValidation="True" ValidationGroup="update" CommandName="UpdateKerdes" CommandArgument='<%# Eval("kerd_id") %>' ImageUrl="~/App_Themes/Theme1/update.png" Height="25" ToolTip="Módosítás" />
<asp:ImageButton ID="btnCancelKerdes" runat="server" CausesValidation="false" CommandName="Cancel" ImageUrl="~/App_Themes/Theme1/cancel.png" Height="25" ToolTip="Mégsem" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="btnEditKerdes" runat="server" CausesValidation="false" CommandName="EditKerdes" CommandArgument='<%# Eval("kerd_id") %>' ImageUrl="~/App_Themes/Theme1/edit.png" Height="25" ToolTip="Szerkesztés" />
<asp:ImageButton ID="btnSelectKerdes" runat="server" CausesValidation="false" CommandName="SelectKerdes" CommandArgument='<%# Eval("kerd_id") %>' ImageUrl="~/App_Themes/Theme1/select.png" Height="25" ToolTip="Kiválasztás" />
<asp:ImageButton ID="btnDeleteKerdes" runat="server" CausesValidation="false" CommandName="DeleteKerdes" CommandArgument='<%# Eval("kerd_id") %>' ImageUrl="~/App_Themes/Theme1/delete.png" Height="25" ToolTip="Törlés" OnClientClick="return confirm('Biztosan törölni akarja ezt a kérdést és a hozzátartozó válaszokat?');" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="btnInsertKerdes" CausesValidation="true" ValidationGroup="insert" runat="server" OnClick="btnInsertKerdes_Click" ImageUrl="~/App_Themes/Theme1/insert.png" Height="25" ToolTip="Új kérdés rögzítése" />
<!--AG dropdown list -->
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
</FooterTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FF9900" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="insert" runat="server" ForeColor="Red" ShowMessageBox="true" ShowSummary="false" />
<asp:ValidationSummary ID="ValidationSummary2" ValidationGroup="update" runat="server" ForeColor="Red" ShowMessageBox="true" ShowSummary="false" />
<asp:ValidationSummary ID="ValidationSummary3" ValidationGroup="insertValasz" runat="server" ForeColor="Red" ShowMessageBox="true" ShowSummary="false" />
<asp:ValidationSummary ID="ValidationSummary4" ValidationGroup="updateValasz" runat="server" ForeColor="Red" ShowMessageBox="true" ShowSummary="false" />
</td>
</tr>
<tr>
<td colspan="3">
<br />
<asp:GridView ID="gvValaszok" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="vala_id" DataSourceID="odsValaszok" ShowFooter="True" ForeColor="#333333" GridLines="None" EmptyDataText="Nincsenek még válaszok megadva!" HorizontalAlign="Center" OnRowCommand="gvValaszok_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="vala_id" InsertVisible="False" SortExpression="vala_id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("vala_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("vala_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="vala_kerd_id" SortExpression="vala_kerd_id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("vala_kerd_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("vala_kerd_id") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblNewValaKerdId" runat="server" Text='<%# Bind("vala_kerd_id") %>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Válaszok" SortExpression="vala_valasz">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("vala_valasz") %>' MaxLength="250" size="100"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("vala_valasz") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txbNewValasz" runat="server" MaxLength="250" size="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTextBox3" runat="server" ErrorMessage="Kötelező az új választ megadni" ValidationGroup="insertValasz" ControlToValidate="txbNewValasz" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Helyes-e?" SortExpression="vala_helyes">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("vala_helyes") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("vala_helyes") %>' Enabled="false" />
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="cbxNewHelyes" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="btnUpdateValasz" runat="server" CausesValidation="True" CommandName="UpdateValasz" CommandArgument='<%# Eval("vala_id") %>' ImageUrl="~/App_Themes/Theme1/update.png" Height="25" ToolTip="Módosítás" />
<asp:ImageButton ID="btnCancelValasz" runat="server" CausesValidation="false" CommandName="Cancel" ImageUrl="~/App_Themes/Theme1/cancel.png" Height="25" ToolTip="Mégsem" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="btnEditValasz" runat="server" CausesValidation="false" CommandName="Edit" ImageUrl="~/App_Themes/Theme1/edit.png" Height="25" ToolTip="Szerkesztés" />
<asp:ImageButton ID="btnDeleteValasz" runat="server" CausesValidation="false" CommandName="DeleteValasz" CommandArgument='<%# Eval("vala_id") %>' ImageUrl="~/App_Themes/Theme1/delete.png" Height="25" ToolTip="Törlés" OnClientClick="return confirm('Biztosan törölni akarja ezt a választ?');" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="btnInsertValasz" CausesValidation="true" ValidationGroup="insertValasz" runat="server" OnClick="btnInsertValasz_Click" ImageUrl="~/App_Themes/Theme1/insert.png" Height="25" ToolTip="Új kérdés rögzítése" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</td>
</tr>
</table>
The code behind this:
public partial class Kerdesek_Valaszok : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsertKerdes_Click(object sender, EventArgs e)
{
odsKerdesek.InsertParameters["KerdTrenId"].DefaultValue = ddlTreningek.SelectedValue;
odsKerdesek.InsertParameters["KerdKerdes"].DefaultValue = ((TextBox)gvKerdesek.FooterRow.FindControl("txbNewKerdes")).Text;
odsKerdesek.Insert();
gvKerdesek.DataBind();
}
protected void btnInsertValasz_Click(object sender, EventArgs e)
{
if (gvKerdesek.SelectedRow != null)
{
odsValaszok.InsertParameters["ValaKerdId"].DefaultValue = ((Label)gvValaszok.Rows[0].FindControl("Label3")).Text;
odsValaszok.InsertParameters["ValaValasz"].DefaultValue = ((TextBox)gvValaszok.FooterRow.FindControl("txbNewValasz")).Text;
odsValaszok.InsertParameters["ValaHelyes"].DefaultValue = ((CheckBox)gvValaszok.FooterRow.FindControl("cbxNewHelyes")).Checked.ToString();
odsValaszok.Insert();
gvValaszok.DataBind();
} else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Nincs a kérdés kiválasztva!');", true);
}
}
protected void ddlTreningek_DataBound(object sender, EventArgs e)
{
odsKerdesek.SelectParameters["TrenId"].DefaultValue = ddlTreningek.SelectedValue;
odsKerdesek.Select();
}
protected void ddlTreningek_SelectedIndexChanged(object sender, EventArgs e)
{
odsKerdesek.SelectParameters["TrenId"].DefaultValue = ddlTreningek.SelectedValue;
odsKerdesek.Select();
gvKerdesek.SelectRow(-1);
}
protected void gvKerdesek_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectKerdes")
{
string tmp = e.CommandArgument.ToString();
int selind = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
odsValaszok.SelectParameters["KerdId"].DefaultValue = e.CommandArgument.ToString();
odsValaszok.Select();
gvKerdesek.SelectedIndex = selind;
gvKerdesek.DataBind();
} else if (e.CommandName == "DeleteKerdes")
{
KerdesekDataAccessLayer.DeleteKerdes(Convert.ToInt32(e.CommandArgument));
gvKerdesek.DataBind();
} else if (e.CommandName == "EditKerdes")
{
int rowindex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
gvKerdesek.EditIndex = rowindex;
gvKerdesek.DataBind();
} else if (e.CommandName == "UpdateKerdes")
{
int rowindex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
int kerd_id = Convert.ToInt32(e.CommandArgument);
odsKerdesek.UpdateParameters["KerdId"].DefaultValue = ((Label)gvKerdesek.Rows[rowindex].FindControl("Label1")).Text;
odsKerdesek.UpdateParameters["KerdKerdes"].DefaultValue = ((TextBox)gvKerdesek.Rows[rowindex].FindControl("TextBox1")).Text;
odsKerdesek.Update();
gvKerdesek.EditIndex = -1;
gvKerdesek.DataBind();
}
}
protected void gvValaszok_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteValasz")
{
ValaszokDataAccessLayer.DeleteValasz(Convert.ToInt32(e.CommandArgument));
gvValaszok.DataBind();
} else if (e.CommandName == "UpdateValasz")
{
int rowindex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
int vala_id = Convert.ToInt32(e.CommandArgument);
odsValaszok.UpdateParameters["ValaId"].DefaultValue = ((Label)gvValaszok.Rows[rowindex].FindControl("Label1")).Text;
odsValaszok.UpdateParameters["ValaValasz"].DefaultValue = ((TextBox)gvValaszok.Rows[rowindex].FindControl("TextBox1")).Text;
odsValaszok.UpdateParameters["ValaHelyes"].DefaultValue = ((CheckBox)gvValaszok.Rows[rowindex].FindControl("CheckBox1")).Checked.ToString();
odsValaszok.Update();
gvValaszok.EditIndex = -1;
gvValaszok.DataBind();
}
}
}
As i told before, the problem is the following. Here is an image for the look and for the asp code. I made an alternative solution, but i really want this too.
The image
I need a code for this:
The dropdown list must contain a folder file names, and after i choose one of them, the text box on the left get filled with the path for the file like this:
~/Dokuments/10001/Teszt/Slide01.JPG
The 10001 can be changed by the top dropdown list, which contains the different test names. If you select a test, then the path changes automatically, so it is not a problem.
And then you can't change the textbox's content only one way, when you choose the default element from the dropdown list.

Edit button and next page link button on same gridview row

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>

One Button to update GridView

I want to update all rows of a GridView one time by clicking one button:
aspx:
</asp:ObjectDataSource>
<asp:ListBox ID="ListBoxRoles" runat="server" DataSourceID="ObjectDataSourceAllRoles"
AutoPostBack="true" DataTextField="RoleName" DataValueField="RoleID"
OnSelectedIndexChanged="ListBoxRoles_SelectedIndexChanged">
</asp:ListBox>
<asp:ObjectDataSource ID="ObjectDataSourceRolePermissions" runat="server"
TypeName="RolePermission" SelectMethod="GetRolePermissionByRoleID"
UpdateMethod="UpdateRolePermission">
<SelectParameters>
<asp:ControlParameter ControlID="ListBoxRoles" Name="RoleID" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="ListBoxRoles" Name="RoleID" PropertyName="SelectedValue"
Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridViewRolePermission" SkinID="GridViewSecurity" runat="server" style="width: 100%;"
HeaderStyle-Height="50" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ObjectDataSourceRolePermissions">
<Columns>
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="PermissionName" SortExpression="PermissionName" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxRead" runat="server" Checked='<%#Bind("Read") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxUpdate" runat="server" Checked='<%#Bind("Update") %>' />
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
<asp:Button runat="server" ID="ButtonUpdateRolePermissions" Text="Save" OnClick="ButtonUpdateRolePermissions_Click" />
I want to update all rows of the gridview once the ButtonUpdateRolePermissions clicked.
I tried:
protected void ButtonupdateRolePermissions_Click(object sender, EventArgs e)
{
foreach(GridViewRow RolePermission in this.GridViewRolePermission.Rows)
{
this.ObjectDataSourceRolePermissions.Update();
}
}
but the update method not take Update and Read fields into consideration.

Nested GridView - How to trigger the Child GridView Button Click event

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
}
}

Categories

Resources