Asp Gridview Edit columns issue : Checkbox does not update columns - c#

I am fairly new to Asp.Net. I have been trying to Edit a column value in a gridview but upon checking one of the two checkboxes (Yes or No), it does not update anything.
Sharing Gridview code below :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#000000" BorderStyle="Solid"
BorderWidth="2px" CellPadding="6" DataKeyNames="film_data" DataSourceID="SqlDataSource" ForeColor="White"
GridLines="Vertical" onrowdatabound="GridView1_RowDataBound" AllowSorting="True">
<AlternatingRowStyle BackColor="#CCCCC" />
<Columns>
<asp:CommandField ShowEditButton="True">
<ControlStyle ForeColor="Blue" />
</asp:CommandField>
<asp:BoundField DataField="film_title" HeaderText="MOVIE TITLE" ReadOnly="True" SortExpression="film_title" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="film_release" HeaderText="RELEASE DATE" ReadOnly="True" SortExpression="release_date" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:CheckboxField DataField="Yes" HeaderText="YES" SortExpression="Yes" />
<asp:CheckboxField DataField="No" HeaderText="NO" SortExpression="No" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle />
</asp:GridView>
This is the GridView1_RowDataBound snippet from .cs file.
public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
....
What am I missing here ? Upon clicking Edit, and the Yes or No checkboxes. nothing gets updated. There is no error either. Kindly assist.

In Gridview change the commandfield and checkbox control like this in your code
<asp:TemplateField HeaderText="Yes/No">
<ItemTemplate>
<asp:Literal ID="ltrchkboxVal" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "chkboxval").ToString() %>'></asp:Literal>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnchkboxval" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "chkboxval").ToString() %>'/>
<asp:CheckboxList ID="chkyesboxval" runat="server" Width="40">
<asp:ListItem> Yes</asp:ListItem>
<asp:ListItem> No</asp:ListItem>
</asp:CheckboxList >
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" CancelText="Cancel" ControlStyle-CssClass="small"
EditText="Optimise" HeaderText="Optimise" ShowEditButton="true" UpdateText="Apply" ItemStyle-Width="30px">
</asp:CommandField>
IN c# code
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
DivMessage.Visible = false;
GridView1.EditIndex = e.NewEditIndex;
//Bind gridview
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string yesnoval = (GridView1.Rows[e.RowIndex].FindControl("chkyesboxval") as CheckBoxList).SelectedItem.Value;
// update new values and Bind gridview
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Bind gridview
}
Add events for the gridview control
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"

Related

Exclude headers for ItemStyle- Width for gridview?

I´m trying to play around ItemStyle- Width and but this only works if i use Attribites.Add("Style"...) in my page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
...
}
gwActivity.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
}
However this is not a problem. My issue is that the column headers are affected in the ItemStyle-Width:... and i do NOT want that to be the case. Can i only play around with the rows and not the headers?
<%--************************ Gridview section ************************--%>
<asp:GridView ID="gwActivity" runat="server" AutoGenerateColumns="False" OnRowCommand="gwActivity_RowCommand" CssClass="gwActivity" >
<Columns>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID"></asp:BoundField>
</Columns>
<HeaderStyle BackColor="#E6E6E6" Font-Bold="false" Font-Names="Arial" ForeColor="#000000" />
<%-- <AlternatingRowStyle BackColor="#E6E6E6" /> --%>
</asp:GridView>
try this..
<asp:GridView ID="gwActivity" runat="server"
AutoGenerateColumns="False" OnRowCommand="gwActivity_RowCommand"
CssClass="gwActivity" >
<HeaderStyle BackColor="#E6E6E6" Font-Bold="false" ForeColor="#000000" Font-Names="Arial />
<Columns>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID"></asp:BoundField>
<asp:BoundField DataField="ActivityID" HeaderText="ActivitID" HeaderStyle-Font-Bold="false"
HeaderStyle-Font-Size="12px" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="25">
<HeaderStyle Font-Bold="False" Font-Size="12px" />
<ItemStyle Font-Size="12px" HorizontalAlign="Left" Width="200px" /> //set width you want..
</asp:BoundField>
</Columns>
</asp:GridView>

I needed to move row from a gridview to a database table How to?

everyone. I needed your help. I am creating an online workshop registration system. I have a inicale gridview that show the all the available workshop for registration during the festival time. I am using a session and check box to get the workshop that the person want into the "shopping cart" on the next page. what I needed to do is after righting a incial order written to an order table in my SQL database. What I am having issues with is when it write the registration data that is stored in an other gridview for the shopping cart. I am writing each row as a new entry into a order detail table. when I do this the program does something very odd. It seem to write all the data from the a gridview that holds all the workshop information on the first page.
code for writing the shopping cart row to the order detail table:
protected void btn_Submit_Click1(object sender, EventArgs e)
{
foreach (GridViewRow row in Basket.Rows)
{
Label lblCatCode = (Label)row.FindControl("lblCatCode");
Label lblTitle = (Label)row.FindControl("lblTitle");
SqlConnection conBasket = new SqlConnection("Data Source=JONS\\SQLEXPRESS;Initial Catalog=OFFFV2;Integrated Security=True");
SqlCommand cmdBasket = new SqlCommand("Insert into OrderDetail(OrderID,CatalogeCode,Title)Values(#OrderID,#CatCode,#Title)", conBasket);
cmdBasket.Parameters.AddWithValue("#OrderID", lblOrderNumber.Text);
cmdBasket.Parameters.AddWithValue("#CatCode", lblCatCode.Text);
cmdBasket.Parameters.AddWithValue("#Title", lblTitle.Text);
try
{
conBasket.Open();
cmdBasket.ExecuteNonQuery();
conBasket.Close();
}
catch (Exception ex)
{
lbl_Error.Text = "A database error has orrued. <br <br />" + "Message: " + ex.Message;
}
}
}
Here is the code for the gridview
<asp:GridView ID="Basket" runat="server" AutoGenerateColumns="False" DataKeyNames="WorkshopID" DataSourceID="BasketData" EnableViewState="False" ShowFooter="True" OnRowCreated="Basket_RowCreate" CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:CheckBox ID="cb_BasketRemove" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cataloge Code" SortExpression="CatalogeCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CatalogeCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCatCode" runat="server" Text='<%# Eval("CatalogeCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<strong>
Total Price:
</strong>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<asp:Label ID="Price" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Literal ID="TotalPrice" runat ="server" />
</strong>
</FooterTemplate>
</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>
I understand that my last post was unclear and was closed. I am at a loss as why the program is doing what it is doing. I am wondering if it is in connection with the way I get the data from the first gridview to the shopping cart.
Here is the code for the session for the selected row
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btt_Select_Click(object sender, EventArgs e)
{
var selectedWorkshop = GridView1.Rows.Cast<GridViewRow>().Where(row => ((CheckBox)row.FindControl("RegisteredWorkshop")).Checked).Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
if (Session["cart"] == null)
{
Session["cart"] = selectedWorkshop;
}
else
{
var cart = (List<string>)Session["cart"];
foreach (var workshop in selectedWorkshop)
cart.Add(workshop);
Session["cart"] = cart;
}
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("RegisteredWorkshop");
if (cb.Checked)
cb.Checked = false;
}
}
protected void btt_CheckOut_Click(object sender, EventArgs e)
{
if (Session["cart"] != null)
Response.Redirect("Checkout.aspx");
}
}
The code for the select gridview from where the registrant select from the various workshops:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" CellPadding="4" DataKeyNames="WorkshopID" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText ="Register">
<ItemTemplate>
<asp:CheckBox ID="RegisteredWorkshop" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FestivalYear" HeaderText="Festival Year" />
<asp:BoundField DataField="CatalogeCode" HeaderText="Catalog Code" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="WSDescription" HeaderText="Workshop Description" />
<asp:BoundField DataField="DayOffered" HeaderText="Day Offered" />
<asp:BoundField DataField="Lenght" HeaderText="Workshop Lenght" />
<asp:BoundField DataField="BeginingTime" HeaderText="Start Time" />
<asp:BoundField DataField="Price" HeaderText="Workshop Fee" />
<asp:BoundField DataField="WorkshopID" HeaderText="Workshop Id" Visible="False" />
</Columns>
I may be wrong but it looks like your variable naming is off (confusing basket and cart?).
You might need to do something like this:
protected void btn_Submit_Click1(object sender, EventArgs e)
{
var Basket = (List<string>)Session["cart"]; // ADD THIS LINE
foreach (GridViewRow row in Basket.Rows)
{

Store many Values In single variable and session that variable to next page and convert into string

This Is my Gridview (sorry image not been uploading on stackExchange).
Headertext(ProductID | Image | Price | Stock | LinkButton(name =ADDtocart)
when user click add to cart button corresponding product id will got from using
asp html code :-
<asp:Content ID="Content1" ContentPlaceHolderID="main" runat="server" >
<asp:GridView ID="GridView3" DataKeyNames="ProductId" OnPageIndexChanging="GridView1_PageIndexChanged" AllowSorting="True" AllowPaging="True" PageSize="5" runat="server" CellPadding="4" EnableModelValidation="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" GridLines="Vertical" PagerStyle-HorizontalAlign="Center" Height="700px" Width="800px" ForeColor="Black" >
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="ProductID" />
<asp:ImageField DataImageUrlField="ProductImage" HeaderText="ProductImage" AlternateText="Image Not Found" ControlStyle-Height="150px" ControlStyle-Width="150px" >
<ControlStyle Height="150px" Width="150px"></ControlStyle>
<FooterStyle Height="30px" Width="30px" />
<ItemStyle BorderStyle="None" BorderWidth="1px" Height="30px" Width="30px" Wrap="False" />
</asp:ImageField>
<asp:BoundField DataField="ModelName" HeaderText="ModelName" >
<ControlStyle Width="30px" />
</asp:BoundField>
<asp:BoundField DataField="Brand" HeaderText="Brand" />
<asp:BoundField DataField="Stock" HeaderText="Stock" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:TemplateField HeaderText="Click Button To Buy">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Add To Cart</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</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" />
</asp:GridView>
<script>
function myFunction() {
alert("Please Login\n New User Click SignUp Button on Top");
}
</script>
</asp:Content>
Button Event Code
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
string s = gr.Cells[0].Text.Trim();
}
My problem is when user click addtocart
string s store one productid value but i need to store multiple productid example like this(9,10,14) and session that value to nextpage for shoppingcart,
string s;
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
//string s = gr.Cells[0].Text.Trim();
s += gr.Cells[0].Text.Trim() + ";";
}

DropDownList in grid view did not working

Aspx Code:
<asp:GridView ID="PaperReviewing" runat="server" CellPadding="4" EmptyDataText="There is no Submission"DataKeyNames="PaperId" RowStyle-Wrap="true" OnRowDataBound="PaperReviewing_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Action" ControlStyle-Width="200px" >
<ItemTemplate>
<br />
<asp:HyperLink ID="HyperLink3" Target="_blank" ForeColor="Black" onclick="javascript:window.open(this.href,'ManuScript Details','resizable=no,scroll bars=yes,status=no,toolbar=yes,height=500,width=700,left=50,top=40');return true;"
NavigateUrl='<%# String.Format("~/EditorsInChief/DownloadSubFileList.aspx?ID={0}", Eval("PaperId")) %>' runat="server">File Inventory</asp:HyperLink>
<br /><asp:HyperLink ID="HyperLink4" Target="_blank" ForeColor="Black" onclick="javascript:window.open(this.href,'ManuScript Details','resizable=no,scroll bars=yes,status=no,toolbar=yes,height=500,width=700,left=50,top=40');return true;"
NavigateUrl='<%# String.Format("~/EditorsInChief/SelectReviewer.aspx?ID={0}", Eval("PaperId")) %>' runat="server">Select Another Reviewer</asp:HyperLink>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PaperId" HeaderText="PaperId" InsertVisible="False"
ReadOnly="True" SortExpression="PaperId" Visible="False" />
<asp:BoundField DataField="ManuScriptId" HeaderText="ManuScript Number"
SortExpression="ManuScriptId" />
<asp:BoundField DataField="Type" HeaderText="Article Type" SortExpression="Type" />
<asp:TemplateField HeaderText="Article Title" HeaderStyle-Wrap="true" ItemStyle-Wrap="true" ItemStyle-HorizontalAlign="Left" SortExpression="PaperTitle" ControlStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("PaperTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ReviewerName" HeaderText="Reviewer Name" SortExpression="ReviewerName" />
<asp:BoundField DataField="Status" HeaderText="Current Status"
SortExpression="Status" />
<asp:BoundField DataField="CreatedDate" HeaderText="Intital Date Submission" SortExpression="CreatedDate" />
<asp:BoundField DataField="DateReviewInvited" HeaderText="Date Review Invited"
SortExpression="DateReviewInvited" />
<asp:BoundField DataField="DateReviewDue" HeaderText="Date Review Due"
SortExpression="DateReviewDue" />
<asp:TemplateField HeaderText="EIC Decision">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="Choose Option"></asp:ListItem>
<asp:ListItem Text="Accept" Value="Accept"></asp:ListItem>
<asp:ListItem Text="Reject" Value="Reject"></asp:ListItem>
<asp:ListItem Text="Minor Revision" Value="MinorRevision"></asp:ListItem>
<asp:ListItem Text="Major Revision" Value="MajorRevision"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" BorderStyle="Double" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
C# Code :
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)DropDownList1.Parent.Parent);
}
protected void PaperReviewing_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}
}
}
the drop down selectedindexchanged event does not fire. I am trying to do as my drop down value changes at run time , in my database value get updates.
remove the event handler adding code from PaperReviewing_RowDataBound since you are already attached it from the markup
in your page load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBindGrid();
}
}

How to get a e.CommandArgument from a row in a gridview while using a dropdownlist inside it in asp.net c#?

recently I asked a similar question, which you can find in the link below
Using Dropdownlist in a gridview Asp.Net?
I have a gridview that looks like this...
<asp:GridView ID="grdvEventosVendedor" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DatakeyNames="idCita"
EmptyDataText="No Hay Eventos Para Este Vendedor" ForeColor="#333333"
GridLines="None" AllowSorting="True"
onpageindexchanging="grdvEventosVendedor_PageIndexChanging"
onrowcommand="grdvEventosVendedor_RowCommand"
onsorting="grdvEventosVendedor_Sorting" CellSpacing="1"
onrowdatabound="grdvEventosVendedor_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnEdicEvento" runat="server"
CommandArgument='<%# Eval("idCita")%>' CommandName="Edicion"
Height="32px" ImageUrl="~/img/pencil_32.png" Width="32px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server"
CommandName="Borrar"
ImageUrl="~/img/1385_Disable_16x16_72.png"
onclientclick="return confirm('¿Desea eliminar el registro?');"
CommandArgument='<%# Eval("idCita")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cliente" HeaderText="Cliente" InsertVisible="False" ReadOnly="True" SortExpression="Cliente" ItemStyle-Width="50px" />
<asp:BoundField DataField="Empresa" HeaderText="Empresa" InsertVisible="False" ReadOnly="True" SortExpression="Empresa" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Telefono" HeaderText="Telefono" InsertVisible="False" ReadOnly="True" SortExpression="Telefono" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Nextel" HeaderText="Nextel" InsertVisible="False" ReadOnly="True" SortExpression="Nextel" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Tipo" HeaderText="Tipo" InsertVisible="False" ReadOnly="True" SortExpression="Tipo" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Descripcion" HeaderText="Descripcion" InsertVisible="False" ReadOnly="True" SortExpression="Descripcion" ItemStyle-Width="100px"/>
<asp:TemplateField HeaderText="Fecha" SortExpression="Fecha" ItemStyle-Width="50px">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Fecha", "{0:dd/MM/yyyy}")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxFecha" runat="server" Text='<%#Bind("Fecha","{0:dd/MM/yyyy}") %>' ValidationGroup="gpEdicionAgenda">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="HoraInicio" HeaderText="Hora" InsertVisible="False" ReadOnly="True" SortExpression="HoraInicio" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Lugar" HeaderText="Lugar" InsertVisible="False" ReadOnly="True" SortExpression="Lugar" ItemStyle-Width="50px"/>
<%--<asp:BoundField DataField="Estado" HeaderText="Estado" InsertVisible="False" ReadOnly="True" SortExpression="Estado" ItemStyle-Width="50px"/>--%>
<asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstado" runat="server" OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Pendiente</asp:ListItem>
<asp:ListItem>Atendido</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CRM" ItemStyle-Width="25px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnCRM" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="CRM"
ImageUrl="~/img/activar.png" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VM" ItemStyle-Width="25px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnVerMas" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="VerMas"
ImageUrl="~/img/search.png" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" Font-Size="Small" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Larger" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" Font-Size="Small" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
As mentioned in the link, I had to manually add some code, like OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True" and also add this on the cs file...
protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
{
}
I have tried it and it did work, whenever I change the value of a ddl, it does enter to the void int the cs file...
however now I have a question...
I need the Id from the record that it's stored in a specific row (idCita, as in the buttons I have which use a CommandArgument)... that way can use another class I have which would trigger a query that would edit a field called Estado....
However, I noticed that when I use CommandArguments, I use another void called...
protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIdCita = int.Parse(e.CommandArgument.ToString());
if (e.CommandName == "Edicion")
{
//Some Code
}
else if (e.CommandName == "Borrar")
{
//More Code
}
else if (e.CommandName == "CRM")
{
//Even More Code
}
else if (e.CommandName == "VerMas")
{
//....
}
}
with this I can call both e.CommandName which tells me which button I pressed, and e.CommandArgument which is the value for idCita... however I noticed that the ddl void that I previously showed you doesnt have a GridViewCommandEventArgs e, instead it has a EventArgs e...
Is there a way that I can get the value of idCita from the gridview, all the way to the ddl void??
What do I need to add in the gridview code and in the cs file??
Thanks
Refactor you're code something like this - it should get you most of the way there, additionally look at converting the else if statements to a more readable switch case statement...
protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
MeaningfulNameHere(int.Parse(grdvEventosVendedor.DataKeys[gvr.RowIndex]),"Estado");
}
protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
{
MeaningfulNameHere(int.Parse(e.CommandArgument.ToString()),e.CommandName);
}
private void MeaningfulNameHere(int id, string commandName)
{
if (commandName == "Edicion")
{
//Some Code
}
else if (commandName == "Borrar")
{
//More Code
}
else if (commandName == "CRM")
{
//Even More Code
}
else if (commandName == "VerMas")
{
//....
}
else if (commandName == "Estado")
{
}
}

Categories