all my commands are working properly but the problem is
after I execute my Search command then I click the select link button on my GridView, My Gridview is refreshing and select the first row in the grid view and not the one that I searched.. example: first I run the Index Page then the GridView get all the record from database(10 populated record) and show to the index.. (No Problem)
then I type the firstname of the user to search it then i click the search button then the it filtered and show to gridview again here's the image
then I click the "Select" link button from Gridview and this what happened
that's what happened .. I clicked the select link button on "marcy's record" then it refreshed and get the value of RASI's Record on the first row...
but when my click select without searching it worked just fine
here's my code for search button
string strconn;
strconn = "select * from User_TBL_DB where (Firstname like '%'+#search+'%' and Middlename like '%'+#search2+'%' and Lastname like '%'+#search3+'%')";
SqlCommand xp = new SqlCommand(strconn, conn);
xp.Parameters.Add("#search", SqlDbType.NVarChar).Value = firstname_search.Text;
xp.Parameters.Add("#search2", SqlDbType.NVarChar).Value = middlename_search.Text;
xp.Parameters.Add("#search3", SqlDbType.NVarChar).Value = lastname_search.Text;
conn.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
and here's my code for select link button on gridview
row = GridView1.SelectedRow;
firstname_tb.Text = (row.FindControl("lbl_Firstname") as Label).Text;
middlename_tb.Text = (row.FindControl("lbl_Middlename") as Label).Text;
lastname_tb.Text = (row.FindControl("lbl_Lastname") as Label).Text;
age_tb.Text = (row.FindControl("lbl_Age") as Label).Text;
id_tb.Text = (row.FindControl("lbl_ID") as Label).Text;
string gender = (row.FindControl("lbl_Sex") as Label).Text;
if (gender == "FEMALE")
{
female_rb.Checked = true;
male_rb.Checked = false;
}
else
{
male_rb.Checked = true;
female_rb.Checked = false;
}
and here's my codes for ASP on Gridview
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="581px"
onrowcommand="GridView1_RowCommand"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<Columns>
<%--<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Select_button" CommandArgument ='<%# Eval("ID") %>' CommandName="SelectRow" ForeColor="#8C4510" runat="server">Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:CommandField ShowSelectButton="True"></asp:CommandField>
<asp:TemplateField HeaderText="Firstname" SortExpression="Firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Firstname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Firstname" runat="server" Text='<%# Bind("Firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middlename" SortExpression="Middlename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Middlename") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Middlename" runat="server" Text='<%# Bind("Middlename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname" SortExpression="Lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Lastname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Lastname" runat="server" Text='<%# Bind("Lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Age" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex" SortExpression="Sex">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Sex") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Sex" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True"
ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399"
HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True"
ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
i connect it to sqlDatasource1 and disconnect it and leave the code as is. I used Showselectbutton="true"
that's my Problem hope you can help me thank you very much
Hi and thanks in advance,
I am trying to sort a gridview with linq and nothing is happening. I am not getting an error, but no sorting is happening in the view either. I am also using firebug for debugging as well.
asp:
<asp:GridView ID="GridViewRangeSetup" runat="server" AllowSorting="True" OnSorting="Gridview_Sort"
PagerStyle-Mode="NumericPages" AutoGenerateColumns="false" Width="100%" CssClass="gridView"
OnPageIndexChanging="GridViewRangeSetup_PageIndexChanging" AllowPaging="True"
PageSize="20" DataKeyNames="RangeId" OnRowCommand="GridViewRangeSetup_RowCommand"
OnRowEditing="GridViewRangeSetup_RowEditing" OnRowCancelingEdit="GridViewRangeSetup_CancelEditRow"
OnRowUpdating="GridViewRangeSetup_UpdateRow" OnRowDataBound="GridViewRangeSetup_RowDataBound">
<RowStyle CssClass="rowStyle"></RowStyle>
<HeaderStyle CssClass="headerBar" ForeColor="#ffffff"></HeaderStyle>
<AlternatingRowStyle CssClass="altRow" />
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%" HeaderStyle-ForeColor="#f2f2f2"
HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/icon_edit.png" CausesValidation="false"
CommandArgument='<%#Eval("RangeId") %>' CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="~/images/icon_update.png"
CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Update" />
<asp:ImageButton ID="ImageCancel" runat="server" ImageUrl="~/images/icon_cancel.png"
CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Cancel" />
</EditItemTemplate>
<HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" CausesValidation="false" OnClientClick="return DeleleAlert();"
CommandArgument='<%#Eval("RangeId") %>' CommandName="Remove" ImageUrl="~/images/icon_delete.png" />
</ItemTemplate>
<HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" CssClass="textbox" Text='<%# Eval("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country.CountryName">
<EditItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="CountryName" DataValueField="CountryId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State/Province" SortExpression="GeographicRegion.RegionName">
<EditItemTemplate>
<asp:DropDownList ID="ddlRegion" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="RegionName" DataValueField="GeographicRegionId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRegion" runat="server" Text='<%# Bind("RegionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Base/Facility" SortExpression="Base.BaseName">
<EditItemTemplate>
<asp:DropDownList ID="ddlFacility" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="BaseName" DataValueField="BaseId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFacility" runat="server" Text='<%# Bind("BaseName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Map Name" SortExpression="RangeMap.MapName">
<EditItemTemplate>
<asp:TextBox ID="txtMapName" runat="server" CssClass="textbox" Text='<%# Eval("MapName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMapName" runat="server" Text='<%# Eval("MapName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Map">
<HeaderStyle HorizontalAlign="center" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink_Map1" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
Text="">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
Width="50px" Height="50px" />
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Low Latitude" SortExpression="RangeMap.LowLat">
<EditItemTemplate>
<asp:TextBox ID="txtLowLat" runat="server" CssClass="textbox" Text='<%# Eval("LowLat") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLowLat" runat="server" Text='<%# Eval("LowLat") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Low Longitude" SortExpression="RangeMap.LowLong">
<EditItemTemplate>
<asp:TextBox ID="txtLowLong" runat="server" CssClass="textbox" Text='<%# Eval("LowLong") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLowLong" runat="server" Text='<%# Eval("LowLong") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="High Latitude" SortExpression="RangeMap.HighLat">
<EditItemTemplate>
<asp:TextBox ID="txtHighLat" runat="server" CssClass="textbox" Text='<%# Eval("HighLat") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHighLat" runat="server" Text='<%# Eval("HighLat") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="High Longitude" SortExpression="RangeMap.HighLong">
<EditItemTemplate>
<asp:TextBox ID="txtHighLong" runat="server" CssClass="textbox" Text='<%# Eval("HighLong") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHighLong" runat="server" Text='<%# Eval("HighLong") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Button ID="RangeSetup_Status" CssClass="page-btn blue" CausesValidation="false"
CommandArgument='<%#Eval("RangeId") %> ' runat="server" Text="Status" OnClick="btnRangeStatus_Click">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
c#
protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{
//Label2.Text = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
WISSModel.WISSEntities context = new WISSModel.WISSEntities();
String column = e.SortExpression;
IQueryable<dynamic> sortedGridview = ConvertSortDirectionToSql(e.SortDirection) == "ASC" ?
(from r in context.Ranges.AsEnumerable()
where r.isDeleted == false
orderby typeof(WISSModel.Range).GetProperty(column).GetValue(r, null) ascending
select new
{
r.RangeId,
r.Description,
r.Country.CountryName,
r.GeographicRegion.RegionName,
r.Base.BaseName,
r.RangeMap.MapName,
r.RangeMap.MapPath,
r.RangeMap.LowLat,
r.RangeMap.LowLong,
r.RangeMap.HighLat,
r.RangeMap.HighLong
}).AsQueryable<dynamic>() :
(from r in context.Ranges.AsEnumerable()
where r.isDeleted == false
orderby typeof(WISSModel.Range).GetProperty(column).GetValue(r, null) descending
select new
{
r.RangeId,
r.Description,
r.Country.CountryName,
r.GeographicRegion.RegionName,
r.Base.BaseName,
r.RangeMap.MapName,
r.RangeMap.MapPath,
r.RangeMap.LowLat,
r.RangeMap.LowLong,
r.RangeMap.HighLat,
r.RangeMap.HighLong
}).AsQueryable<dynamic>();
//var sortedGridview = context.Ranges.Where("it.isDeleted == false").OrderBy(column);
GridViewRangeSetup.DataSource = sortedGridview.ToList();
//var test = sortedGridview.ToList();
//System.Diagnostics.Debugger.Break();
GridViewRangeSetup.DataBind();
}
Looks like it is not allowed to code order by parameters in string like in dynamic T-SQL. You should replace yout LINQ query with this one to make it work:
String column = e.SortExpression;
IQueryable<dynamic> sortedGridview = ConvertSortDirectionToSql(e.SortDirection) == "ASC" ?
(from r in context.Ranges
where r.isDeleted == false
orderby typeof(Range).GetProperty(column).GetValue(r,null) ascending
select new
{
r.RangeId,
Description = r.Description,
CountryName = r.Country.CountryName,
RegionName = r.GeographicRegion.RegionName,
BaseName = r.Base.BaseName,
r.RangeMap.MapName,
r.RangeMap.MapPath,
r.RangeMap.LowLat,
r.RangeMap.LowLong,
r.RangeMap.HighLat,
r.RangeMap.HighLong
}).AsQueryable<dynamic>() :
(from r in context.Ranges
where r.isDeleted == false
orderby typeof(Range).GetProperty(column).GetValue(r, null) descending
select new
{
r.RangeId,
Description = r.Description,
CountryName = r.Country.CountryName,
RegionName = r.GeographicRegion.RegionName,
BaseName = r.Base.BaseName,
r.RangeMap.MapName,
r.RangeMap.MapPath,
r.RangeMap.LowLat,
r.RangeMap.LowLong,
r.RangeMap.HighLat,
r.RangeMap.HighLong
}).AsQueryable<dynamic>();
Found another solution:
Using the DataSetLinqOperators class and modifying it slightly to allow null values:
protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{
WISSModel.WISSEntities context = new WISSModel.WISSEntities();
var sortedGridview = (from r in context.Ranges
where r.isDeleted == false
select new
{
r.RangeId,
r.Description,
r.Country.CountryName,
r.GeographicRegion.RegionName,
r.Base.BaseName,
r.RangeMap.MapName,
r.RangeMap.MapPath,
r.RangeMap.LowLat,
r.RangeMap.LowLong,
r.RangeMap.HighLat,
r.RangeMap.HighLong
}).ToList();
DataTable sortedTable = sortedGridview.CopyToDataTable();
sortedTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridViewRangeSetup.DataSource = sortedTable;
GridViewRangeSetup.DataBind();
}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
int sort = (int)ViewState["Sort"];
switch (sort)
{
case 0:
newSortDirection = "ASC";
ViewState["Sort"] = 1;
break;
case 1:
newSortDirection = "DESC";
ViewState["Sort"] = 0;
break;
}
return newSortDirection;
}
I have a gridview written in asp.net (c#), the problem is when try to read data from the textbox return null.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Week#/Day"
OnRowDataBound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="false"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="true"
PageSize="4" >
<Columns>
<asp:ButtonField ButtonType="Link" CommandName="Update" text="Update" />
<asp:ButtonField ButtonType="Link" CommandName="Edit" text="Edit"/
<asp:TemplateField HeaderText="Week#/Day" InsertVisible="False"
SortExpression="Week#/Day">
<EditItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#
Eval("Week#/Day") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday" SortExpression="Saturday">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Saturday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%# Bind("Saturday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday" SortExpression="Sunday">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Sunday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Bind("Sunday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monday" SortExpression="Monday">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"
Text='<%# Bind("Monday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%# Bind("Monday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday" SortExpression="Tuesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday"
SortExpression="Wednesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am fill it using datatable on edit call this method
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
and on update call update method
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["All_Topics"];
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox d1, d2, d3, d4, d5;
d1 = (TextBox)row.FindControl("TextBox1");
d2 = (TextBox)row.FindControl("TextBox2");
d3 = (TextBox)row.FindControl("TextBox3");
d4 = (TextBox)row.FindControl("TextBox4");
d5 = (TextBox)row.FindControl("TextBox5");
dt.Rows[row.DataItemIndex]["Saturday"] = d1.Text;
dt.Rows[row.DataItemIndex]["Sunday"] = d2.Text;
dt.Rows[row.DataItemIndex]["Monday"] = d3.Text;
dt.Rows[row.DataItemIndex]["Tuesday"] = d4.Text;
dt.Rows[row.DataItemIndex]["Wednesday"] = d5.Text;
}
the problem is when read data from the textbox it is always null. How can I solve this problem.
Hey please Make sure that do not re-binding the GridView on the PostBack of the page. This may be the cause of problem.
Always bind your grid on page load in side the below code.
if (!Page.IsPostBack ){
// Code to bind the Grid
// BindData();
}
when you are click on Row Update Command it is firstly go to the page load event and on page load if you are not binding your grid like the given manner your grid is rebind and you will get null from text box.
Hope it will helps you.
I have a problem which in theory should be very simple but i spent 3 hours now not being able to figure it out. I have a gridview and lingdatasource and i try to implement custom paging. No matter what i do i seem to be stuck with a pagesize of 25! Code below
Thanks!
aspx page
<asp:GridView ID="gvCustomerList" SkinID="StandardGridView" AutoGenerateColumns="false" DataSourceID="LqCustomerSource"
AllowPaging="false" AllowSorting="false" PageSize="50" runat="server" OnSorting="gvCustomerList_OnSorting"
OnRowCommand="gvSupplierCustomerList_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Customer Id" HeaderStyle-HorizontalAlign="Left" SortExpression="CustomerId">
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" HeaderStyle-HorizontalAlign="Left" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).FirstName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" HeaderStyle-HorizontalAlign="Left" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# ((ITB.DAL.LinqObjects.Customer)Container.DataItem).LastName %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink ID="lnkDetails" Text="Details" NavigateUrl='<%# "~/BackOffice/Customers/CustomerDetails.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ask" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink ID="lnkAsk" Text="Ask" NavigateUrl='<%# "~/BackOffice/Customers/Ask.aspx?CustomerId=" + ((ITB.DAL.LinqObjects.Customer)Container.DataItem).CustomerId %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:LinqDataSource ID="LqCustomerSource" AutoPage="false" runat="server"
OnSelecting="LqCustomerSource_Selecting" AutoSort="False">
</asp:LinqDataSource>
code behind
protected void LqCustomerSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
try
{
CustomerDataManager manager = new CustomerDataManager();
string searchName = "";// txtCustomerName.Text.Trim();
int count = 0;
List<Customer> customerList = manager.GetSelectedCustomers(gvCustomerList.PageIndex * 50, 50, searchName, SortExpression, SortDirectionForCustomers, out count);
//List<CustomerInsuranceInfo> infoList = manager.GetCustomerInsuranceDataFromList(customerList);
lblRows.Text = count.ToString();
e.Arguments.StartRowIndex = 0;
e.Arguments.MaximumRows = 50;
e.Arguments.TotalRowCount = count;
if (customerList == null)
e.Cancel = true;
else
e.Result = customerList;
}
catch (Exception ex)
{
ApplicationLogBO.Log(ex);
// Alert(WebsiteUtil.StandardErrorMessage);
e.Cancel = true;
}
}
Ok, it looks like you need to set it in the code behind in LqCustomerSource_Selecting
gvCustomerList.PageSize = 50
How can I find the controls lblCategoryName and ddlBlogCategory?
Here is my code:
protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
string s = lblAccount.Text;
DropDownList ddlBlogCategory = (DropDownList)e.Row.FindControl("ddlBlogCategoty");
DAO.DAO dao_category = new DAO.DAO();
DataTable dt_blog_cat = dao_category.SelectCategory();
if (ddlBlogCategory != null)
{
// ddlBlogCategory.Items.Clear();
ddlBlogCategory.DataSource = dao_category.SelectCategory();
ddlBlogCategory.DataTextField = "CategoryName";
ddlBlogCategory.DataValueField = "BlogCategoryID";
ddlBlogCategory.DataBind();
}
}
}
<asp:GridView ID="grdRetailStore" runat="server" AutoGenerateColumns="false" DataKeyNames="PortalId" OnRowEditing="grdRetailStore_RowEditing"
OnRowCancelingEdit="grdRetailStore_RowCancelingEdit" OnRowDataBound="grdRetailStore_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AdvertiserName">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PromoCategory">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<%-- <asp:DropDownList ID="" runat="server" DataTextField="Account_Name" DataValueField="Account_Id"
AppendDataBoundItems="true">
<%-- <asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList> --%>
<asp:TextBox ID="txtPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Html Code">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:TextBox ID="txtHtml" runat="server" Text='<%# Bind("HtmlCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHtml" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:DropDownList ID="ddlBlogCategoty" runat="server" DataTextField="CategoryName" DataValueField="BlogCategoryID"
AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<HeaderStyle Width="100px" Wrap="False" />
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnClientClick="return ValidateUpdate();"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Change your code behind and include check of RowState.
protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal)
{
Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
string s = lblAccount.Text;
}
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlBlogCategory = (DropDownList)e.Row.FindControl("ddlBlogCategoty");
if (ddlBlogCategory != null)
{
// ddlBlogCategory.Items.Clear();
ddlBlogCategory.DataSource = dao_category.SelectCategory();
ddlBlogCategory.DataTextField = "CategoryName";
ddlBlogCategory.DataValueField = "BlogCategoryID";
ddlBlogCategory.DataBind();
}
}
DAO.DAO dao_category = new DAO.DAO();
DataTable dt_blog_cat = dao_category.SelectCategory();
}
}