Create controls dynamically on button click - c#

I am trying to create dynamic controls on button click.
HTML:
<asp:LinkButton ID="lnkSrt" runat="server" Text="Multi Sort" ForeColor="Black" Font-Names="Calibri" Font-Size="10pt" />
<ajax:ModalPopupExtender runat="server" ID="mpeSrt"
TargetControlID="lnkSrt" PopupControlID="pnlSrt" CancelControlID="btnClose">
</ajax:ModalPopupExtender>
<asp:UpdatePanel ID="upPanel" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlSort" runat="server" align="center" Style="display: none" BackColor="LightGray">
<asp:Label runat="server" Text="Sort By Column" />
<asp:DropDownList ID="ddlColumnSort" runat="server" AutoPostBack="true" />
<asp:Label runat="server" Text="Order By" />
<asp:DropDownList ID="ddlOrder" runat="server" AutoPostBack="true">
<asp:ListItem Text="Ascending" Value="0"></asp:ListItem>
<asp:ListItem Text="Descending" Value="1"></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lnkAdd" runat="server" Text="Add Sorting" Font-Underline="true" OnClick="lnkAddOrder_Click" Font-Names="Calibri" Font-Size="10pt" ForeColor="Black" />
<asp:Button ID="btnSorting" runat="server" Text="Sort" OnClick="btnSorting_Click" />
<asp:Button ID="btnClose" runat="server" Text="Close" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
CODE:
When clicked on AddSorting I am trying to create dropdown control using below code and the controls doesn't show.
protected void lnkAddSort_Click(object sender, EventArgs e)
{
int index = pnlSort.Controls.OfType<DropDownList>().ToList().Count + 1;
this.Createddl("ddldyn" + index);
}
public void Createddl(string id)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
pnlSort.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
pnlSort.Controls.Add(lt);
}
Image:

pnlSrt is set to display: none so the dynamically added child controls will not show either.

As per the #Yoshi answer. You could set visible true for the pnlSrt control by using pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");
Try this
public void Createddl(string id)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
pnlSort.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
pnlSort.Controls.Add(lt);
pnlSort.Attributes.Add("style", "display:block");
}

Related

Set id for multiple radio buttons list generated by a repeater

Is there a way to set the ID for multiple radiobuttonlists generated via repeater.
ie. ID= rep1 rep2 rep3 rep4
I would like to be able to place my data in multiple columns across a table.
Heres is my code so far:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %></h2>
<h3>Your application description page.</h3>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Text") %>' />
<asp:RadioButtonList ID="ddlAnswer" runat="server" AutoPostBack="True" RepeatDirection="Horizontal">
<asp:ListItem Text="Yes" Value="True" />
<asp:ListItem Text="No" Value="False" />
</asp:RadioButtonList>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Provide more details:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Panel>
</p>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Text] FROM [Questions]"></asp:SqlDataSource>
Behind:
protected void Page_Load(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
RadioButtonList ddlAnswer = item.FindControl("ddlAnswer") as RadioButtonList;
System.Web.UI.WebControls.Panel Panel1 = item.FindControl("Panel1") as System.Web.UI.WebControls.Panel;
if (ddlAnswer.SelectedValue == "False")
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
}
You can just use ClientIdMode="Predictable" attribute in the aspx mark-up to assign different ids to different dropdownlists like this:
<asp:RadioButtonList ClientIDMode="Predictable" ID="ddlAnswer" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"> ...

ListView inside an UpdatePanel not getting rebound

I have a ASP.NET ListView inside an UpdatePanel.
In the ItemTemplate there are 2 buttons with Commands "UpdateCart" & "RemoveCart".
In both the commands, the datasource of ListView (an SQL DataSource) gets rebound.
There is no InsertCommand/UpdateCommand/DeleteCommand on the SqlDataSource
My problem is :
Whenver these buttons are clicked : the datasource does not get refreshed.
however I can see from debug mode that values are indeed passed to the datasource selecting method correctly.
The relevant ASPX Page :
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" DisplayAfter="10" runat="server" AssociatedUpdatePanelID="UpnlMain">
<ProgressTemplate>
<div class="divWaiting">
<asp:Label ID="lblWait" runat="server" Text=" Please wait... " />
<asp:Image ID="imgWait" runat="server" ImageAlign="Middle" ImageUrl="~/Image/AjaxLoading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpnlMain" runat="server">
<ContentTemplate>
<asp:ListView ID="lvMain" runat="server" DataSourceID="sdsMain" DataKeyNames="Code"
OnItemCommand="lvMain_ItemCommand">
<EmptyDataTemplate>
<div class="col-xs-12">
<span>No Items in Cart.</span>
</div>
</EmptyDataTemplate>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("Code") %>' Visible="false" />
Product " <asp:Literal ID="ltrStyleNo" runat="server" Text='<%# Eval("StyleNo") %>' />
Qty : <asp:TextBox ID="txtQty" runat="server" type="number" CssClass="txtQty OnlyNumeric form-control text-center input-sm"
Text='<%# Eval("Qty") %>'></asp:TextBox>
<asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="~/image/update.png" CssClass="btn btnUpdateCart"
CommandName="UpdateCart" AlternateText="Update Qty" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/image/delet.png" CssClass="btn"
CommandName="DeleteCart" AlternateText="Remove Item" />
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="sdsMain" runat="server" ConnectionString="<%$ ConnectionStrings:CurrentConnectionString %>"
SelectCommand="pGetItemsForCart" SelectCommandType="StoredProcedure" OnSelecting="sdsMain_Selecting"
UpdateCommand="NoUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The Code Behind Code :
protected void sdsMain_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters[0].Value = GetCartCodes(); //gives some values in csv from Session ..
}
protected void lvMain_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "UpdateCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
TextBox txtQty = e.Item.FindControl("txtQty") as TextBox;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "DeleteCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "RefreshCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
BL.StyleSearchHelper.PriceItem(Page, lblCode.Text);
BindListView();
}
}
void BindListView()
{
sdsMain.DataBind();
lvMain.DataBind();
}
Try replace your buttons outside the updatepanel. Page will not getting refresh when the buttons are in updatepanel. sorry i can't make comment so I post here as an answer.

ASP.NET C# GridView - Focus on RowEditing

I have a large dataset binding to a gridview that is inside of a 300px height restricted DIV tag. When I select a row and trigger the edit function the page reloads the data (as desired) into the gridview and takes me back to the top regardless of how far I may have scrolled down.
Is there a way ideally only using asp.net c# to focus on the row that is being edited. I don't mind if this involves the row being placed at the top of the grid while in edit mode if that is required.
Below is CountryGrid.aspx
<%# Page Language="C#" MasterPageFile="~/MasterPageSimple.master" AutoEventWireup="true"
CodeFile="CountryGrid.aspx.cs" EnableEventValidation="false" Inherits="CountryGrid"
Title="JFA Admin Portal - OP49 Country Update" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<center>
<div class="headlanding">
<asp:Label ID="lb_Welcome" runat="server" CssClass="labelLargeFont" Text="Label">OP49 - Country Update</asp:Label>
<br />
<br />
<br />
<asp:Label ID="Label4" runat="server" CssClass="labelLargeFont" >You are currently modifying: </asp:Label>
<asp:Label ID="Country" runat="server" CssClass="labelLargeFont" />
<br />
<asp:Label ID="Label1" runat="server" CssClass="labelLargeFont" >For </asp:Label>
<asp:Label ID="Season" runat="server" CssClass="labelLargeFont" />
<br />
<br />
</div>
<br />
<div style="text-align: center">
<asp:Label ID="Error_Dashboard" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>
<asp:Label ID="ErrorAccess" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>
</center>
</div>
<asp:Panel ID="Panel1" runat="server" Font-Size="Large" CssClass="Grid_Panel" GroupingText="OP49 - Update Grid" Font-Bold="true">
<center>
<div style="max-height: 550px; overflow: auto;">
<asp:GridView ID="CountryGridView" DataKeyNames="i_SK_Accom" runat="server" Postback="False"
AutoGenerateColumns="False" CssClass="mGrid" RowStyle-CssClass="normal" Width="95%"
OnRowEditing="CountryGridView_RowEditing"
OnRowCancelingEdit="CountryGridView_RowCancelingEdit"
OnRowUpdating="CountryGridView_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Accom Code">
<ItemTemplate>
<asp:Label ID="lb_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
</EditItemTemplate>
<HeaderStyle Width="80px" />
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Accom Name">
<ItemTemplate>
<asp:Label ID="lb_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
</EditItemTemplate>
<HeaderStyle Width="200px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="OP49 Required?">
<EditItemTemplate>
<asp:DropDownList ID="txt_OP49" runat="server" SelectedValue='<%# Bind("OP49_Required") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_OP49" runat="server" Text='<%# Bind("OP49_Required") %>' />
</ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Weekly">
<EditItemTemplate>
<asp:DropDownList ID="txt_Weekly" runat="server" SelectedValue='<%# Bind("Weekly") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_Weekly" runat="server" Text='<%# Bind("Weekly") %>' />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Daily">
<EditItemTemplate>
<asp:DropDownList ID="txt_Daily" runat="server" SelectedValue='<%# Bind("Daily") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_Daily" runat="server" Text='<%# Bind("Daily") %>' />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" >
<ItemTemplate>
<asp:ImageButton ID="bt_Edit" runat="server" CommandName="Edit" AlternateText="Edit" ImageUrl="~/Images/Edit.gif" CssClass="ImageButton" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="bt_Update" runat="server" CommandName="Update" AlternateText="Update" ImageUrl="~/Images/save.png" CssClass="ImageButton" />
<asp:ImageButton ID="bt_Cancel" runat="server" CommandName="Cancel" AlternateText="Cancel" ImageUrl="~/Images/cancel.png" CssClass="ImageButton" />
</EditItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" Wrap="False" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<asp:ImageButton ID="Home" runat="server" ImageUrl="~/Images/home.png" CssClass="ImageButtonLarge" AlternateText="Home" ToolTip="Home" OnClick="Click_Home" />
</center>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
And the code behind page:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public partial class CountryGrid : System.Web.UI.Page
{
SnowballInterface conn = new SnowballInterface();
SnowballInterface CurSeason = new SnowballInterface();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
if (Request["i_FK_CountryID"] == null)
{
Response.Redirect("Default.aspx");
}
#region Get Accom Data
SqlCommand EditCOComm = new SqlCommand("IFACE_JFA_COUNTRY", conn.sbConn);
EditCOComm.CommandType = CommandType.StoredProcedure;
EditCOComm.Parameters.Add("#Statement", SqlDbType.Char).Value = "CountryDetails";
EditCOComm.Parameters.Add("#i_FK_CountryID", SqlDbType.Int).Value = Request["i_FK_CountryID"].Trim().ToString();
try
{
conn.sbConn.Open();
SqlDataReader EditCOReader;
EditCOReader = EditCOComm.ExecuteReader();
// Download default values
while (EditCOReader.Read())
{
Country.Text = Convert.ToString(EditCOReader["Country_Name"]).Trim();
Season.Text = Convert.ToString(EditCOReader["Season_Name"]).Trim();
}
EditCOReader.Close();
}
catch (Exception)
{
}
finally
{
conn.sbConn.Close();
}
#endregion
if (!IsPostBack)
{
BindCountryGrid();
}
}
protected void CountryGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
CountryGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindCountryGrid();
}
protected void CountryGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
CountryGridView.EditIndex = -1;
//Bind data to the GridView control.
BindCountryGrid();
}
protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label Textbox_Accom = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
Label Textbox_Accom_Name = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
DropDownList Textbox_OP49 = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_OP49");
DropDownList Textbox_Weekly = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Weekly");
DropDownList Textbox_Daily = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Daily");
SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
commEditConsultant.CommandType = CommandType.StoredProcedure;
commEditConsultant.Parameters.Add("#Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
commEditConsultant.Parameters.Add("#Page", SqlDbType.VarChar).Value = "OP49";
commEditConsultant.Parameters.Add("#PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
commEditConsultant.Parameters.Add("#Season_Name", SqlDbType.VarChar).Value = Season.Text;
commEditConsultant.Parameters.Add("#Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
commEditConsultant.Parameters.Add("#i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
commEditConsultant.Parameters.Add("#OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
commEditConsultant.Parameters.Add("#Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
commEditConsultant.Parameters.Add("#Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;
conn.sbConn.Open();
commEditConsultant.ExecuteNonQuery();
CountryGridView.EditIndex = -1;
Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
conn.sbConn.Close();
BindCountryGrid();
}
private void BindCountryGrid()
{
SqlCommand CountryGridSelect = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
CountryGridSelect.CommandType = CommandType.StoredProcedure;
CountryGridSelect.Parameters.Add("#Statement", SqlDbType.VarChar).Value = "CountryGridSelect";
CountryGridSelect.Parameters.Add("#i_FK_CountryID", SqlDbType.VarChar).Value = Request["i_FK_CountryID"].Trim().ToString();
try
{
conn.sbConn.Open();
SqlDataReader CountryGridSelectReader;
CountryGridSelectReader = CountryGridSelect.ExecuteReader();
CountryGridView.DataSource = CountryGridSelectReader;
CountryGridView.DataBind();
CountryGridSelectReader.Close();
}
finally
{
conn.sbConn.Close();
}
}
protected void Click_Home(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?i_FK_SeasonID=" + Request["i_FK_SeasonID"].Trim().ToString());
}
}
You can set MaintanScrollPositionOnPostback to true in page directives on your aspx page.
<%# Page Language="C#" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeBehind="Gridview.aspx.cs" Inherits="TestWebsite.Gridview" %>

Access the textbox of dynamically created table in listview of user control page?

I'm creating web template using asp.net and C#.
in my user control page i have to create the table dynamically inside the list view. I just read the data from XML file then retrieve the name and number of columns and rows of each table. While I'm creating the table I assign the name and id to each cell. In this dynamic table once the user click on the edit button of each row all the cells of this row will change to textbox. then the user can change the data of this textboxes,
but in my update function I cannot access to these textboxes by id and store the data in database.
Below is my dynamic table code which is created at the ItemDataBound function:
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
TableRow tr_DataBound = new TableRow();
TableCell tc_DataBound = new TableCell();
for (int i = 1; i <= pkCounter + nonPkCounter; i++)
{
//tc_DataBound = new TableCell();
tc_DataBound = new TableCell();
TextBox tb = new TextBox();
//(TextBox)e.Item.FindControl("td_EditTemp" + i);
tb.Width = 110;
tb.Text = rowView[i - 1].ToString();
tb.ID = "td_EditTemp" + i;
tc_DataBound.Controls.Add(tb);
tc_DataBound.CssClass = "th_ItemTemplate";
tr_DataBound.Cells.Add(tc_DataBound);
}
tr_DataBound.Cells.Add(tc_DataBound);
Table table_Lv_ItemTemplate = (Table)e.Item.FindControl("Table_Lv_ItemTemplate");
table_Lv_ItemTemplate.Rows.Add(tr_DataBound);
My listview code is:
<asp:ListView ID="lv_Uc_Module" runat="server"
onitemediting="lv_Uc_Module_ItemEditing"
onitemcanceling="lv_Uc_Module_ItemCanceling"
onitemdeleting="lv_Uc_Module_ItemDeleting"
OnItemDataBound="lv_Uc_Module_ItemDataBound"
OnSorting="lv_Uc_Module_Sorting">
<LayoutTemplate>
<asp:Table runat="server" ID="table_Lv_Layout">
<asp:TableRow runat="server" ID="tr_Table_Layout">
<asp:TableCell runat="server" ID="td_Table_Layout">
<asp:Table runat="server" ID="itemPlaceholderContainer">
<asp:TableRow runat="server" ID="tr_Table_IphContainer">
<asp:TableHeaderCell runat="server">
<asp:PlaceHolder ID="th_Ph_Lv_header" runat="server"></asp:PlaceHolder>
</asp:TableHeaderCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server" ID="tr_Validate_Table_Layout">
<asp:TableCell runat="server" ID="td_Validate_Table_Layout" HorizontalAlign="Center" BackColor="#CCCCCC">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGEditTmp" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:DataPager ID="lv_DataPager" runat="server" PagedControlID="lv_Uc_Module" PageSize="25" OnPreRender="lv_DataPager_PreRender">
<Fields>
<asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowLastPageButton="true" FirstPageImageUrl="~/Images/First.png" LastPageImageUrl="~/Images/Last.png" NextPageImageUrl="~/Images/Next.png" PreviousPageImageUrl="~/Images/Previous.png" />
<asp:TemplatePagerField>
<PagerTemplate>
<span style="color:Blue;">
</span>
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:Table runat="server" ID="Table_Lv_ItemTemplate"></asp:Table>
</asp:TableCell>
<asp:TableCell runat="server">
<asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="" CssClass="btn_Edit" CausesValidation="True" Visible="false" />
<asp:Button ID="btn_Delete" runat="server" CommandName="Delete" Text="" CssClass="btn_Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this item?');" Visible="false" />
</asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:ListView>
I'm using below method in my update function at the code behind to access the data changed at the textbox:
TextBox tb = (lv_Uc_Module.Items[e].FindControl("td_EditTemp" + i + "_" + e)) as TextBox;
Could you please guide me how to access these textboxes data. Appreciate your consideration.
Have you looked at using the asp:repeater?
It makes creating dynamic controls much simpler especially if you use the 'Data_Bound' Event.
Have a look here - repeater example.

OnCheckedChanged event not firing

I have a GridView with a column of checkboxes (the rest of the GridView is being populated from a database). I'm using AJAX to perform different functions, and I'm wondering if i'm just not calling the OnCheckedChanged event in the right place. Should it be wrapped in some sort of UpdatePanel? I'm still really new to how all of this works...basically what I'm aiming for is to change a bit value in my database when a checkbox is checked. I know the logic of how to do that, I just don't know if I'm addressing my OnCheckedChanged event the right way.
.CS
protected void CheckBoxProcess_OnCheckedChanged(Object sender, EventArgs args)
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
OrderBrowser.Text += "CHANGED";
}
}
.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
<asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
<asp:ListItem Value="lName">Last Name</asp:ListItem>
<asp:ListItem Value="state">State</asp:ListItem>
<asp:ListItem Value="zip">Zip Code</asp:ListItem>
<asp:ListItem Value="cwaSource">Source</asp:ListItem>
<asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label runat="server" ID="searchLabel" Text="Search For: " />
<asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
<asp:Button ID="searchButton" runat="server" Text="Search" />
</div>
<div>
<asp:UpdatePanel ID = "up" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID = "orderByList"
EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlId="searchButton" EventName="Click" />
</Triggers>
<ContentTemplate>
<div align="center">
<asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName"
onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
autogenerateselectbutton = "true"
selectedindex="0">
<SelectedRowStyle BackColor="Azure"
forecolor="Black"
font-bold="true" />
<Columns>
<asp:TemplateField HeaderText="Processed">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" runat="server" Enabled="true" OnCheckedChanged = "CheckBoxProcess_OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:TextBox ID="OrderBrowser" columns="100" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true">
</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Try turning AutoPostBack on for the checkbox control.
<asp:CheckBox ID="CheckBoxProcess" runat="server" Enabled="true" OnCheckedChanged = "CheckBoxProcess_OnCheckedChanged" AutoPostBack="true" />
This maybe the reason your method isn't being called.
Please try to check the value with the DB:
Checked='<%# Convert.ToBoolean(Eval("Processed")) %>'

Categories