C# GridView get textbox input - c#

I have a problem that is most likely easy but I'm missing something small.
I have looked at numerous entries on Stack Overflow with the same question but I must be doing something wrong.
I am trying to get the value of a textbox in a GridView after typing into it. I want to take that value and update a SQL field.
Here is my GridView:
<asp:GridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SALESID" HeaderStyle-HorizontalAlign="CENTER" HorizontalAlign="CENTER" CellPadding="4" ForeColor="#333333" GridLines="Both" AllowSorting="true" AllowPaging="true" PageSize="100" OnRowDataBound="ASPxGridView1_RowDataBound" OnSorting="ASPxGridView1_Sorting" OnRowUpdating="ASPxGridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<%-- 0 This is e.Row.Cells[0].Text --%>
<asp:BoundField DataField="SALESID" HeaderText="SALES ORDER" SortExpression="SALESID" HeaderStyle-CssClass="padLeft" HeaderStyle-Width="125" ItemStyle-Width="115">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 1 --%>
<asp:BoundField DataField="ITEMID" HeaderText="PART NUMBER" HeaderStyle-HorizontalAlign="Center" SortExpression="ITEMID" ItemStyle-Width="150" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 2 --%>
<asp:BoundField DataField="NAME" HeaderText="PART NAME" HeaderStyle-HorizontalAlign="Center" SortExpression="NAME" ItemStyle-Width="320">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 3 --%>
<asp:BoundField DataField="CUSTACCOUNT" HeaderText="ACC" HeaderStyle-HorizontalAlign="Center" SortExpression="CUSTACCOUNT">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 4 --%>
<asp:BoundField DataField="SALESNAME" HeaderText="CUSTOMER" HeaderStyle-HorizontalAlign="Center" SortExpression="SALESNAME" ItemStyle-Width="350" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 5 --%>
<asp:BoundField DataField="SHIPPINGDATECONFIRMED" HeaderText="SHIPPING DATE" HeaderStyle-HorizontalAlign="Center" SortExpression="SHIPPINGDATECONFIRMED" ItemStyle-Width="100" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 6 --%>
<asp:BoundField DataField="QTYORDERED" HeaderText="QTY ORDERED" HeaderStyle-HorizontalAlign="Center" SortExpression="QTYORDERED" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 7 --%>
<asp:BoundField DataField="REMAINSALESPHYSICAL" HeaderText="QUANTITY REMAINED" HeaderStyle-HorizontalAlign="Center" SortExpression="REMAINSALESPHYSICAL" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 8 --%>
<asp:BoundField DataField="AVAILPHYSICAL" HeaderText="AVAIL PHYSICAL" HeaderStyle-HorizontalAlign="Center" SortExpression="AVAILPHYSICAL" ItemStyle-Width="80" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 9 --%>
<asp:HyperLinkField DataTextField="WMSLOCATIONID" HeaderText="LOCATION" HeaderStyle-HorizontalAlign="Center" DataNavigateURLFields="ITEMID" DataNavigateURLFormatString="~\Location.aspx?ITEMID={0}" Target="_blank" SortExpression="WMSLOCATIONID">
<ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
<%-- 10 --%>
<asp:HyperLinkField DataTextField="INPROCESS" HeaderText="IN PROCESS" HeaderStyle-HorizontalAlign="Center" DataNavigateUrlFields="ITEMID" DataNavigateUrlFormatString="~\InProcess.aspx?ITEMID={0}" Target="_blank" SortExpression="INPROCESS">
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
<%-- 11 --%>
<asp:BoundField DataField="PRD" HeaderText="PRD" HeaderStyle-HorizontalAlign="Center" SortExpression="PRD" ItemStyle-Width="135">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 12 --%>
<asp:TemplateField HeaderText="SHIPTODAY" ItemStyle-Width="90">
<ItemTemplate>
<asp:TextBox ID="SHIPTODAY" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%-- 13 --%>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="False" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Here is my gridview _RowUpdating method:
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Debug.WriteLine("row updating");
int index = Convert.ToInt32(e.RowIndex);
Debug.WriteLine("Index: " + index);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TableCell itemIDSelected = selectedRow.Cells[1];
TableCell rowPRD = selectedRow.Cells[11];
TableCell rowShipToday = selectedRow.Cells[12];
TableCell rowNotes = selectedRow.Cells[13];
string itemIdSelectedText = itemIDSelected.Text;
string rowPRDSelected = rowPRD.Text;
string rowShipTodaySelected = rowShipToday.Text;
string test = ((TextBox)selectedRow.FindControl("NOTES")).Text;
string test1 = ((TextBox)ASPxGridView1.Rows[index].FindControl("NOTES")).Text;
TextBox test2 = ASPxGridView1.Rows[index].FindControl("NOTES") as TextBox;
string test2text = test2.Text;
//string rowNotesSelected = rowNotes.Text;
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(rowPRDSelected);
//Debug.WriteLine(rowShipTodaySelected);
Debug.WriteLine(test1);
Debug.WriteLine(test);
Debug.WriteLine(test2text);
//Debug.WriteLine(selectedRow.Cells[0].Text);
//Debug.WriteLine(selectedRow.Cells[1].Text);
//Debug.WriteLine(selectedRow.Cells[2].Text);
//Debug.WriteLine(selectedRow.Cells[3].Text);
//Debug.WriteLine(selectedRow.Cells[4].Text);
//Debug.WriteLine(selectedRow.Cells[5].Text);
//Debug.WriteLine(selectedRow.Cells[6].Text);
//Debug.WriteLine(selectedRow.Cells[7].Text);
//Debug.WriteLine(selectedRow.Cells[8].Text);
//Debug.WriteLine(selectedRow.Cells[9].Text);
//Debug.WriteLine(selectedRow.Cells[10].Text);
Debug.WriteLine(selectedRow.Cells[11].Text);
//Debug.WriteLine(selectedRow.Cells[12].Text);
//Debug.WriteLine(selectedRow.Cells[13].Text);
Debug.WriteLine("-----------");
//Debug.WriteLine(rowShipTodaySelected);
//Debug.WriteLine(rowNotesSelected);
}
When I run this and I type something in, the debug writeline's show.
I know i'm on the correct line because the index is correct per the line I am clicking the Update button on.
Sample debug output from above looks like this:
row updating
Index: 5
D31948P01
PRD-00030521
PRD-00030521
I cannot seem to get the value of the NOTES TextBox field.
As you can see with my testing I've tried using a FindControl and I've tried having it as a TextBox and also trying to get it directly to a string.
I've set breakpoints and each time I do it the test variable is null.
I'm missing something and I do not know what.
To summarize:
On non-text fields I can pull the data from the field.
On TextBox fields I cannot even using the FindControl method.
What I want to do:
Get the TextBox field value in the GridView so I can update the database.
StackOverflow links I've tried before asking this question:
c# Get value (text) of a bound textbox in a gridview
How to get value of TextBox of GridView in C#?
Manipulate textbox on gridview C#
Get TextBox value from GridView cell
ASP.net C# Gridview ButtonField onclick event
Thanks in advance. I've spent over 6 hours of attempting this without results. I don't understand it well enough yet. If there are any tutorials or videos explaining GridView's please let me know what they are so I can understand better.

The reason why RowUpdating method cannot find the text inside the textbox is most likely because of the postback. Clicking update button causes a post back to server. Before ASPxGridView1_RowUpdating is called, there is an a call to re-build the gridview ie.: ASPxGridView1.DataBind(). To avoid this, wrap the databind in an if (!Page.IsPostBack)
Below is a basic working example
<asp:GridView ID="ASPxGridView1"
runat="server"
AutoGenerateColumns="False"
OnRowUpdating="ASPxGridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// This code will not be reached if you click update button.
var data = new string[] { "a", "b", "c" };
ASPxGridView1.DataSource = data;
ASPxGridView1.DataBind();
}
}
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TextBox txtNotes = (TextBox)selectedRow.FindControl("NOTES");
Debug.WriteLine(txtNotes.Text);
}

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>

Update GridViewRow on button click of that row

I have a GridView with checkbox, dropdown and button as templatefields.
On button click I have to update the entire row in the data base.
This is my aspx:
<asp:GridView runat="server" ID="Gdv" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true"
OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="2%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/>
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" HeaderText="Customer Id" ItemStyle-HorizontalAlign="Center" DataField="CustomerID" SortExpression="CustomerID">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Firm's Name" ItemStyle-HorizontalAlign="Center" DataField="Firm's Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Retailer Name" ItemStyle-HorizontalAlign="Center" DataField="Retialer Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Pesticide Licence No." ItemStyle-HorizontalAlign="Center" DataField="Pesticide Licence No" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP1" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP2" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button runat="server" ID="BtnUpdateRow" Text="Update" ToolTip="Update This Record?" OnClick="BtnUpdateRow_Click" BackColor="#336699" ForeColor="#ffffff"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the updatebutton event
protected void BtnUpdateRow_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow grow = (GridViewRow)btn.NamingContainer;
//btn.
CheckBox chkbx = (CheckBox)grow.FindControl("chkRow");
if (chkbx.Checked != true)
{
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Red;
labelErrormessage.Text = "Please Check/Uncheck the CheckBox and Click Update";
return;
}
else
{
/*--Code to update the row's record in the edatabase.--*/
??
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}
}
How do I read the row in which the button has been clicked and update its dropdownlist selected values in the database?
If you have some sort of identification data displayed in the gridview you could get the value from the row's cell like this grow.Cells[0].Text then find the record in the database and update its values.
You could also hide the id for example if this is information you dont want to show to the user. The row will still be located at index 0 but hidden.
Sample:
else
{
var record = queryDatabase(grow.Cells[0].Text);
//update record values
updateRecord(record);
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}

Get link button text in gridview from delete button

I have a gridview with paging function.
the code below is my aspx code for gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SAP No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"
Text='<%#Eval("SAPNO") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PARTNO" HeaderText="Part No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">
</asp:BoundField>
<asp:BoundField DataField="PARTDESC" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="MINQTY" HeaderText="Min Qty" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="QOH" HeaderText="QOH" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">
</asp:BoundField>
<asp:BoundField DataField="CATEGORY" HeaderText="Category" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:BoundField DataField="EQUIPMENT" HeaderText="Equipment" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">
</asp:BoundField>
<asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.JPG" ControlStyle-Height="20px" ControlStyle-Width="50px" HeaderText="Delete">
<HeaderStyle Width="10%" HorizontalAlign="Left" /></asp:CommandField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="<--" PreviousPageText="<" NextPageText=">" LastPageText="-->" />
<PagerStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
At the back end code, i tried to retrieve the first column and specific rows but it throw me empty. the code below is how i retrieve.
int index = Convert.ToInt32(e.RowIndex);
string sapNo1 = GridView1.Rows[index].Cells[0].Text;
string partNo = GridView1.Rows[index].Cells[1].Text;
string partDesc = GridView1.Rows[index].Cells[3].Text;
I try to show all the value for sapNo1, it throw me blank but for partNo it will show the data in the gridview.
anyone has any idea about this?
I am really appreciate your help and comment!
As your first column is a linkbutton so retrieve that as it is then find the value.
string sapNo1 = GridView1.Rows[index].Cells[0].Text; // wrong code to retrieve data from linkbutton in a gridview
Modify this as :
LinkButton linkbtn =(LinkButton)GridView1.Rows[index].FindControl("LinkButton1");
string sapNo1 = linkbtn.Text;
or
string sapNo1 =(GridView1.Rows[index].FindControl("LinkButton1") as LinkButton).Text;
You can't get the controls properties present inside TemplateField like that, you need to find the control like this:-
LinkButton LinkButton1 = (LinkButton)GridView1.Rows[index].Cells[0]
.FindControl("LinkButton1");
After this you can simply fetch the properties of this control:-
string sapNo1 = LinkButton1.Text;
you can get the link button description by the below code
protected void Link_Button_Command(object sender, CommandEventArgs e)
{
try
{
LinkButton button = (LinkButton)sender;
string name = button.ToString();
}
catch (Exception ex)
{
lbl_message.Text = ex.Message;
}
}

how to get data from a gridview

I display customers past orders in a gridview. They can press the Resend button in the last column to resend their order under the condition that the order is paid or past that status.
First the gridview:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" pagesize="10"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" EmptyDataText="You have no orders yet"
GridLines="Vertical" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="#DCDCDC"/>
<Columns>
<asp:BoundField DataField="TD_OrdID" HeaderText="Ord" InsertVisible="False" ReadOnly="True" SortExpression="TD_OrdID" />
<asp:BoundField DataField="TD_OrdDate" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Right" HeaderText="Date" SortExpression="TD_OrdDate" ><ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="TD_PD_PackageName" HeaderText="Package Name" ItemStyle-HorizontalAlign="Left" SortExpression="TD_PD_PackageName" > </asp:BoundField>
<asp:BoundField DataField="TD_OSName" ItemStyle-HorizontalAlign="Right" HeaderText="Status" SortExpression="TD_OSName" > <ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="TD_InvID" HeaderText="Inv" InsertVisible="False" ReadOnly="True" SortExpression="TD_InvID" />
<asp:BoundField DataField="TD_InvPaid" HeaderText="Paid" ItemStyle-HorizontalAlign="Right" SortExpression="TD_InvPaid" ><ItemStyle HorizontalAlign="Right"></ItemStyle></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btnResend" CommandName="Resend" Text="Resend" CommandArgument="<% ((GridViewRow) Container).RowIndex
%>" OnDataBinding="btnResend_DataBinding" />
</ItemTemplate>
</asp:TemplateField>
</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" Font-Size="Small" HorizontalAlign="Center"/>
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
now the databinding to select the resend button when customer paid the order:
protected void btnResend_DataBinding(object sender, EventArgs e)
{
// only set button to enable when customer can resend document!
Button btn = (Button)(sender);
// enable button for status emailed, paid and closed
btn.Enabled = ((Eval("TD_OSName").ToString().Equals("closed")) ||
(Eval("TD_OSName").ToString().Equals("paid")) ||
(Eval("TD_OSName").ToString().Equals("emailed")));
}
which works fine. I am disabling the button. Maybe a better solution is to hide it instead. However this is not my concern.
now the row command to get the "ordId" value from the row where the button is pressed:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Resend")
{
// Retrieve the row index stored in the
// CommandArgument Property
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from Rows collection.
GridViewRow row = GridView1.Rows[index];
// add code
int ordId = Convert.ToInt32(row.Cells[0].Text);
lblMessage.ForeColor = System.Drawing.Color.Green;
//lblMessage.Text = displayMessages.YourDocumentResent() + "OrdID:" + ordId;
//lblMessage.Text = displayMessages.YourDocumentResent();
lblMessage.Text = Convert.ToString(e.CommandArgument);
}
}
However to get the selected row and reading out the order identification from the row is challenging for me. I goggled this over and over and can’t see where I make the mistake.
The line:
int index = Convert.ToInt32(e.CommandArgument)
fails with
Input string was not in a correct format
If found this even on the msdn page of Microsoft?!
If I display the e.commandArgument I see: <% ((GridViewRow) Container).RowIndex %>
Can anyone help so I can get the value OrdId from TD_OrdID from the row the customer has pressed the Resend button?
Thank you in advance.
<% ((GridViewRow) Container).RowIndex %>
You're missing the #.
Try:
<%# ((GridViewRow) Container).RowIndex %>

jquery datepicker date not binding to asp.net textbox in gridview editing/updating (with masterpage)

in the code below, the string sFoodFormReceived1 always gets the value of DateTime.Now. I dont know how to make the date clicked on the datepicker UI bind to the textbox in the gridview gvReservationsWithForms.
protected void gvReservationsWithForms_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvReservationsWithForms.Rows[e.RowIndex];
string sFoodFormReceived1 = ((TextBox)(row.Cells[9].Controls[1])).Text;
DataTable dt = dsReservationsToBind.Tables[0];
dt.Rows[gvReservationsWithForms.EditIndex]["FoodFormReceived"] = new DateTime(System.DateTime.Parse(sFoodFormReceived1));
DataRow dr = dt.Rows[row.DataItemIndex];
var Reservation = new reservation_RoomReservationTableAdapter();
Reservation.Update(dr);
gvReservationsWithForms.EditIndex = -1;
gvReservationsWithForms.DataBind();
}
<asp:Content ID="Content4" ContentPlaceHolderID="Main" Runat="Server">
<script type="text/javascript">
$(document).ready(function ()
{$("input[id$='tbFoodFormReceivedEditItemTemplate']").datepicker();})
</script>
<asp:GridView ID="gvReservationsWithForms" runat="server"
AutoGenerateColumns="False"
DataKeyNames="RoomReservationID" AutoGenerateSelectButton="True"
allowsorting ="True" AutoGenerateEditButton="True" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Vertical" onsorting="gvReservationsWithForms_Sorting1"
onrowcancelingedit="gvReservationsWithForms_RowCancelingEdit"
onrowediting="gvReservationsWithForms_RowEditing"
onrowupdating="gvReservationsWithForms_RowUpdating"
onselectedindexchanged="gvReservationsWithForms_SelectedIndexChanged1"
onrowupdated="gvReservationsWithForms_RowUpdated">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<columns>
<asp:BoundField DataField="RoomReservationID" HeaderText="Confirmation ID" SortExpression="RoomReservationID" Visible="false" />
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ReadOnly="true"/>
<asp:BoundField DataField="BeginDate" HeaderText="Event Begin Date" SortExpression="BeginDate" ReadOnly="true"/>
<asp:BoundField DataField="EndDate" HeaderText="Event End Date" SortExpression="EndDate" ReadOnly="true"/>
<asp:BoundField DataField="BeginTime" HeaderText="Event Begin Time" SortExpression="BeginTime" ReadOnly="true"/>
<asp:BoundField DataField="EndTime" HeaderText="Event End Time" SortExpression="EndTime" ReadOnly="true"/>
<asp:BoundField DataField="Department" HeaderText="Department/Organization" SortExpression="Department" ReadOnly="true"/>
<asp:BoundField DataField="Activity" HeaderText="Activity" SortExpression="Activity" ReadOnly="true"/>
<asp:TemplateField HeaderText="Food Form Received"
SortExpression="FoodFormReceived">
<EditItemTemplate>
<asp:TextBox ID="tbFoodFormReceivedEditItemTemplate" runat="server" CssClass="datepickers"
Text='<%# DateTime.Now%>'> </asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FoodFormReceived") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
</asp:Content>
You may want to instead set the textbox to datetime.now in code, on RowEditing instead. That way, the binding doesn't interfere. It could be that...
also, you may get better performance if you do:
$("#<%= gvReservationsWithForms.ClientID %>")
.find("input[id$='tbFoodFormReceivedEditItemTemplate']").datepicker();
Instead, so you restrict the input controls to just the gridview, and not the entire page.

Categories