i have file upload control in gridview and that gridview is inside update panel
when i try to update gridview everything works but image path from fileupload don't save
please help me...
page.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" CellSpacing="4"
DataKeyNames="pid" ForeColor="Black" ShowHeaderWhenEmpty="True"
GridLines="Horizontal" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Operation">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ForeColor="#94b52c"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" ForeColor="#94b52c"
OnClientClick="return confirm('Are You Sure Want To Delete ?');"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product ID" InsertVisible="False" SortExpression="pid">
<EditItemTemplate>
<asp:Label ID="lblpid" runat="server" Text='<%# Eval("pid") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblproductid" runat="server" Text='<%# Bind("pid") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Detail" SortExpression="pdetail">
<EditItemTemplate>
<asp:TextBox ID="txtproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Image" SortExpression="pimage">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="imgproductimage" runat="server" ImageUrl='<%# Bind("pimage") %>' Height="50px" Width="50px"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F0F0F0" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
here is .cs file
public void bindgrid()
{
string qry = "select pid,pdetail,pimage from productdetail p,categorydetail c where p.cid=c.cid";
GridView1.DataSource = abal.Qry_Fire(qry);
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)GridView1.Rows[e.RowIndex].FindControl("lblpid");
TextBox txtproductdetail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtproductdetail");
FileUpload f = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");
string path = "~/user/product_image/" + f.FileName.ToString();
int msg = abal.Qry_All("update productdetail set pdetail='" + txtproductdetail.Text + "',pimage='" + path
+ "' WHERE pid='" + Convert.ToInt32(l.Text) + "'");
if(msg==1)
f.SaveAs(Server.MapPath(path));
GridView1.EditIndex = -1;
bindgrid();
}
I had same kind of problem and fixed it by using below solution.
Add the following code in your code behind.
Page.Form.Attributes.Add("enctype", "multipart/form-data");
protected void gvLineItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState.Equals(DataControlRowState.Edit))
{
Button btnUpload = e.Row.FindControl("btnUpload") as Button;
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnUpload);
}
}
}
I am able to retrive the fileUpload control value after adding the above code.
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
FileUpload uploadedFile = (FileUpload)dgDocuments.Rows[editIndex].FindControl("UploadFile");
if (uploadedFile.HasFile)
{
uploadedFile.SaveAs(FileUploadURL + "\\Temp\\" + uploadedFile.FileName);
}
}
just add PostBackTrigger after </ContentTemplate> for the FileUploader as below:
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="FileUpload1" />
</Triggers>
</asp:UpdatePanel>
Update if its inside a gridview then you can try the below code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
FileUpload flUpload = e.Row.FindControl("FileUpload1") as FileUpload;
ScriptManager.GetCurrent(this).RegisterPostBackControl(flUpload);
}
Update2 add the event OnRowDataBound in the gridview:
<asp:gridview
id="GridView1"
onrowdatabound="GridView1_RowDataBound"
rest add your code for gridview
add this: Page.Form.Attributes.Add("enctype", "multipart/form-data"); to page load event:
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
..............................
}
and Try This.
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Update panel dosen't work properly with some controls similarly with Fileupload..So try to do it with ajax call which is a better option.
Just Add "Page.Form.Attributes.Add("enctype", "multipart/form-data");" as posted by Jisha Muthuswamy. FileUpload control inside the gridview wrapped by UpdatePanel can never be accessed and thus cannot be set as a PostBackTrigger ControlID. Thanks
Related
I have a grid view which has two autogenerated Edit and delete buttons in first cell itself. The code is as below
<asp:GridView ID="grdStudent" runat="server" DataKeyNames="id" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
OnRowDeleting="grdStudent_RowDeleting" OnRowDataBound="grdStudent_RowDataBound" OnRowEditing="EditGrid" OnRowCancelingEdit="CancelEdit" OnRowUpdating="UpdateGrid"
ForeColor="#333333" CellPadding="4" GridLines="Both" Width="90%"
class="table table-striped table-bordered table-hover" AllowPaging="True"
EnableSortingAndPagingCallbacks="false">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#999999" BorderStyle="Solid" HorizontalAlign="Center"
VerticalAlign="Middle" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"
BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" CssClass="table table-striped table-bordered table-hover" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("first_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFirstName" Text='<%#Eval("first_name") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("last_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("last_name") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact No">
<ItemTemplate>
<asp:Label ID="lblContactNo" runat="server" Text='<%#Eval("phone_no") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtContactNo" Text='<%#Eval("phone_no") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("email") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtAddress" Text='<%#Eval("address") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="active" HeaderText="IsActive" />--%>
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%#Eval("active") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<select id="ddlIsActive" class="dropdown" runat="server">
<option value="1">True</option>
<option value="2">False</option>
</select>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="lblUsername" runat="server" Text='<%#Eval("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtUsername" Text='<%#Eval("username") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblPassword" runat="server" Text='<%#Eval("password") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtPassword" Text='<%#Eval("password") %>' Width="100px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%--<asp:ButtonField ButtonType="Link" --%>
</Columns>
</asp:GridView>
I need to access the delete button in Row Data bound event and add a confirmation attribute to it. I tried the below code but only Edit button is accessed at server side.
protected void grdStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != grdStudent.EditIndex)
{
string text = ((LinkButton)e.Row.Cells[0].Controls[0]).Text;
//text always has the value as "Edit" never "Delete"
if (text == "delete")
{
(e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
//del.OnClientClick = "return confirm('Are you certain you want to delete the record?');";
}
}
// (e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
// //(e.Row.Cells[0].Controls[1] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
//}
//if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == grdStudent.EditIndex)
//{
// (e.Row.Cells[0].Controls[0] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to edit this row?');";
//}
}
How can I access Delete button and add a confirmation message box.
You have to add the attributes as a key/value pair.
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbEdit = e.Row.Cells[0].Controls[0] as LinkButton;
lbEdit.Attributes.Add("onclick", "return confirm('Do you want to edit this row?');");
LinkButton lbDelete = e.Row.Cells[0].Controls[2] as LinkButton;
lbDelete.Attributes.Add("onclick","return confirm('Do you want to delete this row?');");
}
#VDWWD's answer should solve the original problem.
These days, I personally like to use icon/font to look more professional.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="DemoWebForm.GridViewDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<form id="form1" runat="server">
<br />
<div class="container">
<asp:GridView runat="server" ID="grdStudent"
AutoGenerateColumns="False"
OnRowEditing="grdStudent_RowEditing"
OnRowDeleting="grdStudent_RowDeleting"
OnRowDataBound="grdStudent_RowDataBound"
CssClass="table">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton runat="server" ID="EditLinkButton" CommandName="Edit">
<i class="fa fa-pencil"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton runat="server" ID="DeleteLinkButton" CommandName="Delete">
<i class="fa fa-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace DemoWebForm
{
public partial class GridViewDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grdStudent.DataSource = Collection;
grdStudent.DataBind();
}
}
protected void grdStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var d = e.Row.FindControl("DeleteLinkButton") as LinkButton;
d.Attributes.Add("onclick", "return confirm('Do you want to delete this row?');");
}
}
protected void grdStudent_RowDeleting(object sender, GridViewDeleteEventArgs e){}
protected void grdStudent_RowEditing(object sender, GridViewEditEventArgs e){}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public static IList<Item> Collection = new List<Item>
{
new Item {Id = 1, Name = "One"},
new Item {Id = 2, Name = "Two"},
new Item {Id = 3, Name = "Three"}
};
}
}
This will work better for confirmation message box at Client side:
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[2];
if (lb.Text == "Delete")
{
lb.OnClientClick = "return confirm('Are you sure you want to delete this row?');";
}
}
I have a Grid Where I have Id like this:
<div class="row ">
<asp:GridView ID="gvIndex" CssClass="table table-bordered table-responsive" runat="server" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" AutoGenerateEditButton="False" OnRowCommand="gvIndex_RowCommand">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Número" HtmlEncode="false"> //This is my Id Field
<FooterStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Nombre" HeaderText="Nombre" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Direccion" HeaderText="Dirección" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Puesto" HeaderText="Puesto" HtmlEncode="false">
<FooterStyle Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="Fecha" HeaderText="Fecha" HtmlEncode="false">
<FooterStyle Width="175px" />
</asp:BoundField>
<asp:BoundField DataField="CorreoElectronico" HeaderText="Correro Electrónico" HtmlEncode="false">
<FooterStyle Width="350px" />
</asp:BoundField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Edición">
<ItemTemplate>
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton> //There I want to get value of Id
<%--<asp:HyperLink ID="EditarIngreso" BorderStyle="None" OnClick="EditarIngreso_Click" Text="Editar" runat="server">HyperLink</asp:HyperLink>--%>
<%--<a id="EditarIngreso" runat="server" style="text-decoration: underline; border: none">Editar</a>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
As you can see, I want to get value of Id in this line:
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton>
But I don't know what is wrong with this, I try to get it with this method:
protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
{
MyID.Value = e.CommandArgument.ToString();
}
But I just receiving "<%Eval('Id') %>" instead my Id into MyID.Value
Any Help is very appreciate. Regards
You use it like this:
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("Id") %>' runat="server" Text="Button" CommandName="SelectRow" />
And it looks you are using a regular button OnClick also, you need to remove that and use the CommandName in gvIndex_RowCommand
protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectRow")
{
MyID.Value = e.CommandArgument.ToString();
}
}
First missing #
<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<%# Eval('Id') %>" CommandName="SelectRow" CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton>
In Gridview Tag
add DataKeyNames= "Id"
<asp:Gridview id="gvIndex" DataKeyNames= "Id" />
Then OnClick of LinkButton
protected void EditarIngreso_Click(object sender,EventArgs e)
{
LinkButton btn = sender as LinkButton ;
GridViewRow row = btn.NamingContainer as GridViewRow;
string pk = gvIndex.DataKeys[row.RowIndex].Values[0].ToString();
// here PK is your ID
// Convert it to Int32
}
I have a Dto for phone numbers:
namespace Dto
{
public class PhoneNumberDto
{
public Int16 PhoneTypeId { get; set; }
public string PhoneNumber { get; set; }
}
}
and a list of this Dto:
List<PhoneNumberDto> phonenum = new List<PhoneNumberDto>();
also I have a gridview:
<table style="width: 100%">
<tr>
<td style="direction: rtl;"align="center" class="auto-style8">
<asp:UpdatePanel ID="UpdatePanelSpect1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowCommand="GridViewCertificateType_RowCommand" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" Width="100%" Font-Names="Tahoma" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Phone Type
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DDLSpecialty" runat="server" Height="24px" Width="500px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelC1" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:TextBox ID="TextBox_Phone" runat="server"></asp:TextBox> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:ImageButton ID="ImageButtonAddSpec" runat="server" ImageUrl="~/ImageWebForm/add.png" Width="18px" OnClick="ImageButtonAddSpec_Click" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonDel0" runat="server" ImageUrl="~/ImageWebForm/Delete.png" Width="18px" CommandName="Del" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridViewCertificateType" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
Now I want to set data to my dropdown list and text box in Gridview from my list of Dot "phonenum",e.g "PhoneTypeId" set to dropdownlist and phoneNumber set to text box
You can direct bind List to your GridView and then either in the OnRowDataBound event or direct in the template you can do something like this.
First in the template.
<asp:TemplateField>
<HeaderTemplate>
Phone Type
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DDLSpecialty" Selectedvalue='<%#((PhoneNumberDto)Container.DataItem).PhoneTypeId%>' runat="server" Height="24px" Width="500px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelC1" runat="server" Text="Label"
Visible="False"></asp:Label>
<asp:TextBox ID="TextBox_Phone"
Text='<%#((PhoneNumberDto)Container.DataItem).phoneNumber %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Or in the OnRowDataBound event using FindControl() method you can assign value to your control.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PhoneNumberDto dtoPhone = (PhoneNumberDto)e.Row.DataItem;
((TextBox)(e.Row.FindControl("txtPhoneNo"))).Text = dtoPhone.PhoneNumber;
((DropDownList)(e.Row.FindControl("DDLSpecialty"))).SelectedValue = dtoPhone.PhoneTypeId;
}
}
I hope from above 2 option you might get your solution...
I can't get the value. What I do get is a blank value or videoName = " ". How can I get the value of videoName? In the rowdeleting event, I am using Rows(e.Index).Cells(2).Text to get the value but it is blank. Is there another way to get the field, "videoname"?
Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim videoName As String = gridview1.Rows(e.RowIndex).Cells(2).Text
Dim val As String = videoName
If File.Exists(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName) Then
File.Delete(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName)
End If
End Sub
<asp:GridView id="GridView1" runat="server" Width="680px" GridLines="None" DataSourceID="SqlDataSource2" DataKeyNames="id" CellSpacing="1" CellPadding="3" BorderWidth="2px" BorderStyle="Ridge" BorderColor="White" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" EmptyDataText="No Record Found" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="9" >
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" ReadOnly="true" visible="false" />
<asp:BoundField HeaderText="CustomerID" DataField="CustomerID" ReadOnly="true" visible="false"/>
<asp:BoundField HeaderText="VideoName" DataField="VideoName" ReadOnly="true" visible="false"/>
<asp:TemplateField>
<HeaderStyle Width="5%" />
<ItemStyle Width="5%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this video?');"
CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Clip" SortExpression="ThumbName">
<HeaderStyle Width="5%" />
<ItemStyle Width="5%" />
<ItemTemplate>
<div style="margin:2px; width:133px; background-color:rgb(68,68,68); -moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6); -webkit-box-shadow:5px 5px 5px rgba(68,68,68,0.6);box-shadow:5px 5px 5px rgba(68,68,68,0.6); zoom: 1;">
<asp:hyperlink id="link" NavigateUrl='<%# Eval("VideoName", "~/Test/playVideos2.aspx?FileName={0}&Thumb=" + Eval("ThumbName") + "&Duration=" + Eval("Duration"))%>' runat="server">
<asp:image id="img" ImageUrl='<%# String.Format("~/contents/thumbs/{0}",Eval("ThumbName"))%>' width="130" height="80" runat="server" />
</asp:hyperlink>
<asp:Label ID="lblMovieName" Text='<%#Bind("VideoName") %>' runat="server"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<HeaderStyle Width="15%" />
<ItemStyle Width="15%" />
<ItemTemplate>
<asp:Label ID="date" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle Width="75%" />
<ItemStyle Width="75%" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
This is prone to erros when you add or delete a column from gridview in future your code would fail again. I suggest you use a label inside template field for your MovieName field & use FindControl to find your label.
Aspx
<asp:TemplateField HeaderText="Movie Name" >
<ItemTemplate>
<asp:Label ID="lblMovieName" Text='<%#Bind("MovieName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Code behind
protected void gvCustomer_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblMovieName = gvCustomer.Rows[e.RowIndex].FindControl("yourLableControlName") as Label;
// Perform your delete
}
Updated
I found that in your code you have the movieName Visibleproperty set to false. This will prevent it from rendering on page. You can either store values in DataKeys or remove visible=false to fetch desired value.
I am not able to change the page index of the gridview. The server method for OnPageIndexChanging is not at all getting fired. I do not know what am I doing wrong here.
Here's my gridview
<asp:GridView ID="VideoCommentsGrid" runat="server"
OnRowDataBound="VideoCommentsGrid_RowDataBound"
OnPageIndexChanging="VideoCommentsGrid_PageIndexChanging" allowpaging="true"
CssClass="tables"
EmptyDataText="<div class='notice show bottom'>No Comments found.</div>"
AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
<HeaderTemplate>
Approve
<br />
<input id="ChkAllApprovedItems" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkApproval" Checked='<%#Eval("IsApproved").ToString()=="1"?true:false %>' runat="server" />
<asp:Label ID="lblCommentID" runat="server" Text='<%#Eval("CommentId") %>' CssClass="hide"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
<HeaderTemplate>
Reject
<br />
<input id="ChkAllRejectedItems" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkReject" Checked='<%#Eval("IsRejected").ToString()=="1"?true:false %>' runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name" HeaderStyle-Width="70" >
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%#Eval("FirstName")%>'> </asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="150" />
<ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments" HeaderStyle-Width="70" >
<ItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("VideoComment") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="150" />
<ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment Time* ">
<ItemTemplate>
<asp:Label ID="lblCommentDate" runat="server" Text='<%#(Eval("CommentCreatedDate"))%>'> </asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="80" />
<ItemStyle HorizontalAlign="Center" Width="80" />
</asp:TemplateField>
</Columns>
<HeaderStyle Height="30" />
<PagerStyle HorizontalAlign="Center" CssClass="footer" />
<AlternatingRowStyle CssClass="odd" />
</asp:GridView>
My server side code is as follows,
protected void Page_Load(object sender, EventArgs e)
{
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
}
protected void VideoCommentsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
}
protected void VideoCommentsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
VideoCommentsGrid.PageIndex = e.NewPageIndex;
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
hidCheckedValue.Value = string.Empty;
}
protected void LoadCommentsGridView(int PageIndex)
{
SetPageIndex(PageIndex);
LoadDefaultGrid();
}
private void LoadDefaultGrid()
{
VideoCommentsGrid.PageSize = CurrentSchoolDetails.PageViewCount;
IList<Comment> allComments = CommentRepository.GetAllCommentsByVideoID(VideoID);
BindDataControls.BindGridView(VideoCommentsGrid, allComments);
}
Please help me out,
Thanks.
I think this is occurring because you are binding the data to the GridView on every page load, where as you only need to bind the data once initially and then again when the page index changes.
Try changing the code in the Page_Load method like so:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadCommentsGridView(VideoCommentsGrid.PageIndex);
}
}
on the rowdatabound method try to comment this piece of code and see what happen.
//if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;