I have two file upload controls inside GridViewControl for uploading files, I have a button for uploading files inside gridview, I am using its RowCommandEvent for uploading files. How can I get values of fileupload controls in RowCommandEvent?
This is my GridView
<asp:GridView ID="DgFiles" runat="server" AutoGenerateColumns="false" OnRowCommand="DgFiles_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Crime" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="category" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Category") %>' ></asp:Label><br/>
<asp:FileUpload ID="file1" runat="server" /><br />
<asp:FileUpload ID="file2" runat="server" />
<asp:Button ID="btnupload" runat="server" CommandName="upload" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my RowCommandEvent
protected void DgFiles_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "upload")
{
}
}
I have to display records in GridView like structure, with file upload control inside it, If there is alternate way I can do it, kindly suggest.
First,
You have to add a command column with command name upload
second,
You have to find the control inside the row by using parent property.
FileUpload fl1 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File1")
FileUpload fl2 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File2")
Now You can use fl1 and fl2 as your filecontrols.
Related
I am trying to delete rows from ASP.NET GridView and for demo purpose, I am trying to retrieve names from the rows right now. In the ItemTemplate of the GridView, I've included Label that has id as well name to show. Finally added Button control in the GridView to delete specific rows and tried the following:
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
In the code-behind, I've iterated the GridView with a loop to get the individual values, say name of the row as follows:
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdData.Rows)
{
string val = ((Label)row.FindControl("lblName")).Text;
lblMsg.Text = val;
}
}
But unfortunately I get the last name every time I click any row button and here is the screen-shot that I am trying:
Whenever I click any button, it shows the last name Ice-Cream every time. Anything that I missed here?
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" CommandArgument='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
protected void btnShow_Click(object sender, EventArgs e)
{
lblMsg.Text = (sender as Button).CommandArgument;
}
I've a textbox that is present inside a GridView that itself is a part of user control and I'm dynamically loading this user control on page_Init.
The controls in my user control are a DropDown, a TextBox and a GridView.
Now DropDown and TextBox are able to retain values across postback but textboxes inside the GridView are not retaining the values.
Adding User Control on page init:
if (postBackCntrl.Contains("AddUserControlButton"))
{
UserControl newGrid = (UserControl)LoadControl("~/UserControl.ascx");
newGrid.ID = "test" + gridList.Count;
gridList.Add(newGrid);
}
GridView Code: TextBoxes inside GridView ItemTemplate not able to retain values across postback:
<asp:GridView runat="server" ID="grdUser" CssClass="GridTable" Width="90%" AutoGenerateColumns="false"
ShowFooter="true" OnDataBound="MergeGridViewFooter">
<Columns>
<asp:TemplateField HeaderText="ContactUs">
<ItemTemplate>
<asp:TextBox ID="txtContactUs" runat="server" CssClass="textbox" Height="60px" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnaddrow" runat="server" Text="Add Row" CssClass="ui-button jquery-ui" Style="font-size: 12px" OnClick="btnaddrow_Click" />
<asp:Button ID="btnDelRowAddress" runat="server" Text="Delete Row" CssClass="ui-button jquery-ui" Style="font-size: 12px" OnClick="btnDelRowAddress_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Definition">
<ItemTemplate>
<asp:TextBox ID="txtDefinition" runat="server" TextMode="MultiLine" Height="60px" CssClass="textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Height="60px" CssClass="textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have a nested gridview of images that are associated with the parent grid's row on a one to many relationship. This structure is contained in a user control. The images are uploaded with a fileupload control and are stored in a folder on the server. When the image grid's row is deleted, I delete the image from the server in the OnRowDeleting event. What's weird is that when you delete a row, a postback occurs and the OnRowDeleting event fires, the record is deleted from the database, but the row does not go away from the UI until you then delete it a second time. What's even weirder is that when I comment out the code in the OnRowDeleting event, the row deletes immediately. What's going on here?
<%# Control Language="C#" AutoEventWireup="true" CodeFile="OrderItemView.ascx.cs" Inherits="Controls_OrderItemView" EnableTheming="true" EnableViewState="true"%>
<asp:GridView ID="OrderItemList" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id,ConcurrencyId" DataSourceID="OrderItemDataSource"
SkinID="Blue" OnRowDataBound="OrderItemList_RowDataBound"
EnableModelValidation="True" Width="100%"
OnRowUpdating="OrderItemList_RowUpdating"
OnRowUpdated="OrderItemList_RowUpdated" AllowSorting="True" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonConfirmDelete" runat="server"
CausesValidation="False" CommandName="Delete" ImageUrl="../Images/Grid_ActionDelete.gif"
OnClientClick='return confirm("Are you sure you want to delete this Order Item? This cannot be undone.");'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Name" SortExpression="OrderItemName">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OrderItemName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Images">
<ItemTemplate>
<a href="" data-id='<%# Eval("id","imageHeader_{0}") %>' class="expandImages">
<asp:Label ID="Label3" runat="server"
Text='<%# string.Format("{0} Image{1}", Helpers.GetImageCount(new Guid(Eval("Id").ToString())), Helpers.GetImageCount(new Guid(Eval("Id").ToString())) != 1 ? "s" : "") %>'>
</asp:Label>
</a>
<div id='<%# Eval("id","images_{0}") %>' class="imageDisplay">
<asp:GridView ID="gvImages" runat="server" SkinID="Blue"
DataKeyNames="Id" EnableModelValidation="True"
DataSourceID="ImageDataSource" AutoGenerateColumns="False"
ShowHeader="False" onrowdeleting="gvImages_RowDeleting">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonConfirmDeleteImage" runat="server"
CausesValidation="False" CommandName="Delete" ImageUrl="../Images/Grid_ActionDelete.gif"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FileName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<a href="" data-id='<%# Eval("id","image_{0}") %>' onclick='showImage("<%# Eval("ImageUrl") %>"); return false;'>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FileName") %>' ToolTip="Click to Preview Image"></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="color: Blue; font-style: italic;">No Images</div>
</EmptyDataTemplate>
</asp:GridView>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUploadImage" runat="server" Text="Upload" CommandArgument='<%# Eval("Id") %>'
CommandName="orderItemId" oncommand="btnUploadImage_Command"/>
<asp:ObjectDataSource ID="ImageDataSource" runat="server"
DataObjectTypeName="OrderSite.Entities.OrderItemImage"
DeleteMethod="Delete" InsertMethod="Save"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetList"
TypeName="OrderSite.Bll.OrderItemImageManager" UpdateMethod="Save"
SelectCountMethod="SelectCountForGetList">
<SelectParameters>
<asp:Parameter DbType="Guid" Name="orderItemId"/>
</SelectParameters>
</asp:ObjectDataSource>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="padding: 3px 0 3px 3px 3px;">No Order Items Exist</div>
</EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="OrderItemDataSource" runat="server"
DataObjectTypeName="OrderSite.Entities.OrderItem"
DeleteMethod="Delete" InsertMethod="Save"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetList"
TypeName="OrderSite.Bll.OrderItemManager" UpdateMethod="Save"
OnInserting="OrderItemDataSource_OnInserting"
SortParameterName="sortExpression" SelectCountMethod="SelectCountForGetList">
<SelectParameters>
<asp:Parameter DbType="Guid" Name="orderFormId" />
<asp:Parameter Name="sortExpression" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
I commented out any containing updatepanels in the parent page as they can always be the culprit, but there was no effect.
Here is the OnRowDeleting event:
protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Also delete the file from the images folder
OrderItemImage myImage = OrderItemImageManager.GetItem((Guid)e.Keys["Id"]);
if (myImage != null)
{
string path = string.Format("../Images/OrderItemImages/{0}", myImage.OrderItemId.ToString());
if (File.Exists(Server.MapPath(string.Format("{0}/{1}", path, myImage.FileName))))
{
File.Delete(Server.MapPath(string.Format("{0}/{1}", path, myImage.FileName)));
// if there are no images left in folder, then delete it (only deletes empty directory)
if (Directory.GetFiles(Server.MapPath(path), "*.*", SearchOption.TopDirectoryOnly).Length == 0)
Directory.Delete(Server.MapPath(path));
}
OrderItemList.DataBind();
}
}
I've done this before and have had no issues, so any help would be greatly appreciated!
I ended up wrapping the nested GridView ("gvImages"), the FileUpload, and Button in a user control, and now it works great. I didn't change any of the code, just wrapped it all in the user control, so go figure.
i can't get the value of textbox from the footer row of gridview
<asp:GridView ID="GridView1" runat="server" Width="1214px"
AutoGenerateColumns="False" ShowFooter="true"
OnRowCommand="GridView1_RowCommand"
<Columns>
<asp:TemplateField HeaderText="Insert">
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the code behind is........
i can't get the value of textbox from the footer row of gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName==("AddNew"))
{
TextBox txtName =(TextBox) GridView1.FooterRow.FindControl("txtSName");
string strName=txtName.Text; //strName is Empty while i m enters data into the textbox txtSName
}
Posting this comment to help others who may encounter the same issue.
Make sure the grid is not loading again on post back.
code in the page load should look like this
if (!IsPostBack)
{
LoadGrid();
}
Your mark up is biting you.You are having two footer templates within a template field.
Do this
<asp:TemplateField HeaderText="Insert">
<ItemTemplate>
<asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
ImageUrl="~/images/Edit.png"Style="height: 16px" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"
CausesValidation="false" OnClientClick="return confirm('Delete.Are you sure you want to delete?')"
ImageUrl="~/images/DeleteTS.png" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="AddNewImgBtn" runat="server" ImageUrl="~/images/saveHS.png"
ToolTip="Add New" AlternateText="Add New" Width="16px" Height="16px"
CommandArgument="InsertNew" ImageAlign="AbsMiddle" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And then you can gracefully locate your footer row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument=="InsertNew")
{
GridView testGrid=(Gridview)sender;
TextBox txtName =(TextBox)testGrid.FooterRow.FindControl("txtSName");
string strName=txtName.Text;
}
}
See working example Example
I have an ajax tab which inside has a grid, on this grid there is a check box where the user has the ability to check it and assign it to a technician, but for some reason im unable to reference the control, the grid has a total of 4 rows so there is data there, this is my code which is run of a button click
protected void btnAllocate_click(object sender, EventArgs e)
{
foreach (System.Web.UI.Control s in tbHappyCall.Controls)
{
foreach (GridViewRow Row in gvHappyCall.Rows)
{
CheckBox chkAllocate = (CheckBox)Row.FindControl("chkAllocate");
if (chkAllocate.Checked)
{
HyperLink lblOrderID = (HyperLink)Row.FindControl("hyperOrderID");
HappyCallManager objHappyCallManager = new HappyCallManager();
objHappyCallManager.HappyCallAllocated(Convert.ToInt64(lblOrderID.Text), objWebuser.ShortAbbr, ddlAllocateTo.SelectedValue);
//Update database with person details thats are assigned to the Welcome Call
}
}
}
}
when it goes on to the Foreach gridviewrow etc the count is 1 even though there is 4 rows displaying information ?
Can any one shed any light on this? or even better a solution?
Thank you for your time.
UPdate
<ajaxToolkit:TabPanel ID="tbHappyCall" runat="server" HeaderText="Welcome Calls" TabIndex="10">
<ContentTemplate>
<%--OnRowDataBound="gvConfirmOrder_rowDataBound"--%>
<div id="divHappyCall">
<asp:GridView ID="gvHappyCall" runat="server" AutoGenerateColumns="false" class="tablesorter"
EmptyDataText="There is no Record to display." DataKeyNames="OrderID" EnableViewState="false"
OnRowDataBound="gvHappyCall_RowDataBound">
<AlternatingRowStyle CssClass="NormalTextVNC" BackColor="#E2E9E7"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Account Manager">
<ItemTemplate>
<asp:Label ID="lblAccountManager" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AccountManager") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:HyperLink ID="hyperOrderID" runat="server" ForeColor="White" NavigateUrl='<%#Eval("OrderGuid","/Documents/HappyCall.aspx?OrderID={0}") %>'
Text='<%#Eval("OrderID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblCustomerName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="lblCompanyName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dispatch Date">
<ItemTemplate>
<asp:Label ID="lblConnectionDate" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DespatchDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField headertext="Date Called" dataformatstring="{0:dd/MM/yyyy}" datafield="EditedDate" HeaderStyle-Width="100px" />
<asp:TemplateField HeaderText="Allocated To">
<ItemTemplate>
<asp:Label ID="lblAllocatedTo" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AllocatedTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Last Action">
<ItemTemplate>
<asp:Label ID="lblLAstAction" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LastAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAssignWelcomeCall" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:DropDownList runat="server" ID="ddlAllocateTo" CssClass="floatright">
<asp:ListItem>Name of Admin to Allocate</asp:ListItem>
</asp:DropDownList>
<div class="WhiteSpace"></div>
<asp:Button runat="server" ID="btnAllocate" class="floatright" Text="Allocate" OnClick="btnAllocate_click" />
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
For clarification: you don't need to iterate the control collection of your TabPanel to find your GridView, it is available directly and don't confuse it's Controls.Count with the number of GridView.Rows.Count.
Is ViewState disabled somewhere?
Edit: I see that the GridView's ViewState is disabled. I assume that it works when you enable it.