I have a GridView that binds values from the database.
Here is the code:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Button" CommandName="AddToCart" />
</Columns>
</asp:GridView>
I want to pass the command arguments containing values from txtQuantity and ProductID to the code behind on CommandName "AddToCart".
How can I do this?
Bind the values of the ProductID & Quantity in the GridView. In GridView, give like this:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity" Text="<%# Bind("Quantity")%>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Button Text="Button" CommandName="AddToCart" CommandArgument="<%# Eval("Quantity") + "," + Eval("ProductID")%>" />
</Columns>
</asp:GridView>
In GridView RowCommand event, give the below code:
if(e.CommandName == "AddToCart")
{
string[] args = e.CommandArgument.ToString().Split(",");
Decimal Quantity = Convert.ToDecimal(args[0]);
int ProductID = Convert.ToInt32(args[1]);
}
Related
I want to update the text of the btn_Update which is inside the EditTemplate of gridview. For that I wrote the code shown here. But the text is still shown as Reject, even if I am trying to update the text to Approve.
Here's my markup and code:
<asp:GridView ID="rptHotoIPDataInfo" runat="server" AutoGenerateColumns="False" CssClass="table table-bordered nowrap tblrptHotoIPDataInfo"
AllowPaging="True" PageSize="20" OnPageIndexChanging="rptHotoIPDataInfo_PageIndexChanging"
DataKeyNames="SAP_ID" OnRowEditing="rptHotoIPDataInfo_RowEditing" OnRowUpdating="rptHotoIPDataInfo_RowUpdating"
OnRowCancelingEdit="rptHotoIPDataInfo_RowCancelingEdit" OnRowDataBound="rptHotoIPDataInfo_RowDataBound"
OnRowCommand="rptHotoIPDataInfo_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Approved / Reject" ItemStyle-Width="80px"> <%--"Accepted/Rejected (By default checked means approved)"--%>
<ItemTemplate >
<asp:CheckBox ID="chkRow" runat="server" Checked="true" Width="5px" OnCheckedChanged="chkRow_CheckedChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Width="120px">
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Reject" CommandName="Edit" Enabled="false" CssClass="btn btn-blue" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Reject" CommandName="Update" CssClass="btn btn-blue" />
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" CssClass="btn btn-blue" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rejected Remarks" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblRemarks" runat="server" Text='<%#Eval("REJECT_REMARKS") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRemarks" runat="server" Text='<%#Eval("REJECT_REMARKS") %>' Width="150px" CssClass="form-control"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SAP_ID" HeaderText="SAP ID">
<ItemStyle CssClass="labelbold" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="CR_NO" HeaderText="Description" Visible="False">
<ItemStyle CssClass="labelbold" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="R4GSTATE_CODE" HeaderText="State Code">
<ItemStyle CssClass="labelbold" Width="30px" />
</asp:BoundField>
<asp:BoundField DataField="HOTO_STATUS" HeaderText="Hoto Status">
<ItemStyle CssClass="labelbold" Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="CR_Justifications" HeaderText="CR Justifications">
<ItemStyle CssClass="labelbold" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="APPROVE_REJECT" HeaderText="Status">
<ItemStyle CssClass="labelbold" Width="10px" />
</asp:BoundField>
<asp:BoundField DataField="REJECTED_BY" HeaderText="Last Updated by">
<ItemStyle CssClass="labelbold" Width="10px" />
</asp:BoundField>
</Columns>
</asp:GridView>
And the code behind is:
protected void rptHotoIPDataInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow row = rptHotoIPDataInfo.Rows[rowIndex];
string name = row.Cells[9].Text;
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var clickedUserName = clickedRow.Cells[9].Text;
if (clickedUserName == "Approved")
{
clickedButton.Text = "Reject";
}
else if (clickedUserName == "REJECTED")
{
clickedButton.Text = "Approve"; // this statement is not updating the text
}
}
}
I have an <asp:ButtonField> inside a gridview. How do I target the button in the gridview to make it Visible on runtime based on a condition ? I am not able to target it since it doesnt have the ID property. I am stuck here. Here is the code below
<asp:GridView ID="OrdersDataList1" runat="server" DataKeyNames="OrderID" Width="100%" SkinID="Gridview" OnPageIndexChanging="orders_PageIndexChanging"
EmptyDataText="You have no orders." AllowSorting="True" OnSorting="OnSort" AllowPaging="true" PageSize="15" AutoGenerateColumns="False" OnRowCommand="updateStatus">
<Columns>
<asp:BoundField DataField="CustomerUser_ID" HeaderText="UserID" Visible="true" />
<asp:BoundField DataField="OrderID" HeaderText="OrderNo" InsertVisible="False" ReadOnly="True" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" SortExpression="OrderID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField DataField="Base" HeaderText="Base" DataFormatString="{0:C}" SortExpression="Base" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Freight" HeaderText="Freight" DataFormatString="{0:C}" SortExpression="Freight" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:C}" SortExpression="Total" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundField DataField="Products" HeaderText="Products" SortExpression="Products" DataFormatString="{0} product" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" DataFormatString="{0} units" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OrderStatusName" HeaderText="Current Status" SortExpression="OrderStatusName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:BoundField DataField="OrderStatusID" HeaderText="Orderstatusid" Visible="true" />
<asp:BoundField DataField="OrderTracking_ID" HeaderText="TrackingNo" SortExpression="OrderTracking_ID" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<%# GetViewOrderLink(Eval("OrderID").ToString(), Eval("OrderState").ToString())%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Change Status" SortExpression="OrderStatusName">
<ItemTemplate>
<asp:DropDownList ID="OrderStatusDD" runat="server"
DataSourceID="OrdersStatuses" DataTextField="OrderStatusName" DataValueField="OrderStatusID" Visible="false">
</asp:DropDownList>
<asp:SqlDataSource ID="OrdersStatuses" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConn %>"
SelectCommand="SELECT [OrderStatusID], [OrderStatusName] FROM [Orders_Statuses] where OrderStatusID = 2 or OrderStatusID = 8 ORDER BY [OrderStatusName]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Update status" Visible="false" HeaderText="Change Status" />
</Columns>
</asp:GridView>
PS I have the onrowCommand set on the gridview to listen to button click in gridview
You need to create RowCreated event for the gridview example
Before the GridView control can be rendered, a GridViewRow object must be created for each row in the control. The RowCreated event is raised when each row in the GridView control is created. This enables you to provide an event-handling method that performs a custom routine, such as adding custom content to a row, whenever this event occurs.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
OR
<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Reject"
Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>'
CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected Boolean IsOverAgeLimit(Decimal Age) {
return Age > 35M;
}
Reference example
You can use the RowCreated event in the GridView as follows:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
Button btn = (Button) e.Row.Cells[12].Controls[0];
if(1==1)
{
btn.Visible = true;
}
}
((BoundField)grv_selec.Columns[1]).DataFormatString = "{0:N2}";
i have a button in my gridview with the ID "btnApprove". what i want is when the user clicks the button, the row "Status" will be updated to 'Approved'. how can i acheive that? one row will only be updated when the button is clicked depending on the transaction number
here is my aspx code.
<asp:UpdatePanel ID="panel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img style="cursor:pointer" src ="../Images/Icons/plus2.png" />
<asp:Panel ID ="pnlDetails" runat="server" Style="display: none">
<asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<Columns>
<%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%>
<asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" />
<asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" />
<asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" />
<asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" />
<asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" />
<asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" />
<%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="TransactionID" HeaderText="Transaction Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="DateFiled" HeaderText ="Date Filed" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqName" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqCompany" HeaderText="Company" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBranch" HeaderText="Branch" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBU" HeaderText="Business Unit" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqDept" HeaderText="Department" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqSection" HeaderText="Section" />
<asp:BoundField ItemStyle-Width="150px" DataField="TransStatus" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" CssClass="btn btn-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="CornflowerBlue" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Update here is my data source of my GridView
public void showTable()
{
Utility u = new Utility();
string conn = u.connect();
SqlConnection connUser = new SqlConnection(conn);
SqlDataAdapter adp = new SqlDataAdapter("select * from MosefTransaction where TransStatus = 'Pending'", connUser);
DataTable dt = new DataTable();
connUser.Open();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Add CommandArgument='<%# Container.DataItemIndex %>' to your button and in your code behind you can get the row that raised the event on Gridview's RowCommand event
<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="ApproveTransaction" CommandArgument='<%# Container.DataItemIndex %>'/>
In your codebehind subscribe to gridview's row command event.
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ApproveTransaction")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvInfo.Rows[index];
string cellText = row.Cells[2].Text;
//Update your data in database here and rebind the gridview to updated data
}
}
I have a gridView which works nicely until I open a new window by clicking a button. When the new window is opened the values in one of the columns 'lblTotalPrice' gets erased. 'lblTotalPrice' is a template field, which is calculated in MealList_RowDataBound (quantity * price). The footer for the column, however is not affected when the new window is opened (also gets its value in MealList_RowDataBound).
Also the font-size of the data-bound columns are changed when the new window is opened.
Any ideas of what might cause this? When stepping through the code there are no other lines executed after btnEnvComment_Click
When gridView is databound again, everything is back to normal.
The gridView:
<rwg:BulkEditGridView ID="MealList" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="Vertical" CellPadding="4"
BackColor="White" DataKeyNames="ItemId,ProductId" SaveButtonID="SaveButton"
DataSourceID="SqlMealItems" style="float:left"
OnRowDataBound="MealList_RowDataBound" OnRowCreated="MealList_RowCreated" CssClass="gridView">
<AlternatingRowStyle CssClass="MealListItemAlt" BackColor="#a6dbed" />
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="Prod #" HeaderStyle-Wrap="false" ReadOnly="True" >
<HeaderStyle Wrap="False" Width="60px"></HeaderStyle>
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="Namn" ReadOnly="True" ItemStyle-Wrap="False" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Antal" ControlStyle-CssClass="numeric" >
<ControlStyle CssClass="numeric" Width="30px"></ControlStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Enhet">
<ItemTemplate>
<asp:DropDownList ID="DropDownUnit" OnSelectedIndexChanged="DropDownUnit_SelectedIndexChanged"
AutoPostBack="False" runat="server" DataTextField="Unit" DataValueField="Unit">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="70px" />
</asp:TemplateField>
<asp:BoundField HeaderText="รก pris" ReadOnly="True" ControlStyle-CssClass="rightAlign" ItemStyle-Wrap="False">
<ControlStyle CssClass="numeric" ></ControlStyle>
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Totalt" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:label ID="lblTotalPrice" runat = server></asp:label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="80px" />
<FooterTemplate>
<asp:Label ID="lblTotalPricePerMeal" runat="server"></asp:Label>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" Font-Bold="true" Font-Overline="True" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Unit" HeaderText="h" ReadOnly="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:image runat="server" ID="imgBlob" Width="24"></asp:image>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblError"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="MealListFooter" />
<HeaderStyle CssClass="MealListHead" />
</rwg:BulkEditGridView>
The code for the button that open the new window:
protected void btnEnvComment_Click(object sender, EventArgs e)
{
int offsetPos = 125; // set placement of new window
String URL = "EnvComment.aspx?MealID=" + Session["MealId"];
String responsCommand = "window.open('" + URL + "','_blank','height=400,width=300,top=" + ",left=" + offsetPos.ToString() + offsetPos.ToString() + ",titlebar=1')";
Response.Write("<script>");
Response.Write(responsCommand);
Response.Write("</script>");
}
I'm use grid view for show recent messages...there use datasource...
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="586px" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField HeaderText="show" ShowSelectButton="True" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
<asp:BoundField DataField="user_name" HeaderText="user_name"
SortExpression="user_name" />
<asp:BoundField DataField="sender_mail" HeaderText="sender_mail"
SortExpression="sender_mail" />
<asp:BoundField DataField="message" HeaderText="message" ReadOnly="True"
SortExpression="message" ControlStyle-Width="70px" ControlStyle-Height="25">
<ControlStyle Height="20px" Width="50px" />
<HeaderStyle Height="10px" Width="70px" />
<ItemStyle Height="20px" HorizontalAlign="Left" Width="70px" />
</asp:BoundField>
</Columns>
</asp:GridView>
there in my database if message is too long then it show in one field...
ex:-msg is 'hi how are you'
it show full msg......but i show data only 'hi how...'
i'm also try set width and height but not work.
You can use a template field instead of the boundfield.
<asp:TemplateField >
<HeaderTemplate>Message</HeaderTemplate>
<ItemTemplate>
<%# Eval("message").ToString().Substring(0,10) %>
</ItemTemplate>
<EditItemtemplate>
<asp:textbox id="Textbox1"
text='<%#Eval("message")%>'
width="90"
runat="server"/>
</Edititemtemplate>
</asp:TemplateField>
Here we are taking a substring of the message (10 characters only). you can modify to suit your needs.
You can do this by adding method to your code that take the msg from DB or just string then you make on that string whatever you want(cut the string at specific index then add ...). And that method then return the processed string back
public string cutString(string msg)
{
int msgLength = 100;
return msg.Substring(0, msgLength) + "...";
}
<asp:Label runat="server" Text='<%# cutString(Eval("message").ToString())%>' />