How to read user control values? - c#

I want to get values from user control
I tried but loop comes out
Code:
.aspx
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnSaveVisa" Text="Save" runat="server" OnClick="btnSaveVisa_Click" />
AddVisaUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server"></asp:DropDownList></td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
<td>
</td>
</tr>
</table>
</div>
.aspx.cs
public void generateControls()
{
for (int i = 0; i < int.Parse(ViewState["ControlCount"].ToString()); i++)
{
Label lbl = new Label();
string count = Convert.ToString(i + 1);
lbl.Text = "Visa" + count;
rpt1.Controls.Add(lbl);
rpt1.Controls.Add(LoadControl("VisaUserControl.ascx"));
rpt1.Controls.Add(new LiteralControl("<BR>"));
}
}
protected void btnAddVisa_Click(object sender, EventArgs e)
{
ViewState["ControlCount"] = int.Parse(ViewState["ControlCount"].ToString()) + 1;
generateControls();
}
//Here is the problem when I read the values from control the loop comes out
private void saveData()
{
for (int i = 0; i < this.rpt1.Controls.Count; i++)
{
if (this.rpt1.Controls[i] is TextBox)
{
TextBox txtserial = (TextBox)this.rpt1.Controls[i];
string value = txtserial.Text;
}
}
}
protected void btnSaveVisa_Click(object sender, EventArgs e)
{
saveData();
}
Any ideas? Thanks in advance

public List<string> NoOfControls
{
get
{
return ViewState["NoOfControls"] == null ? new List<string>() : (List<string>)ViewState["NoOfControls"];
}
set
{
ViewState["NoOfControls"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
GenerateControls();
}
private void GenerateControls()
{
foreach (string i in NoOfControls)
{
var ctrl = (AddVisaUserControl)LoadControl(#"AddVisaUserControl.ascx");
ctrl.ID = i;
this.AddVisaPlaceHolder.Controls.Add(ctrl); // Add in placeholder
}
}
//Adding controls to Place Holder
protected void AddButton_Click(object sender, EventArgs e)
{
List<string> temp = null;
var uc = (AddVisaUserControl)this.LoadControl(#"AddVisaUserControl.ascx");
string id = Guid.NewGuid().ToString();
uc.ID = id;
temp = NoOfControls;
temp.Add(id);
NoOfControls = temp;
AddVisaPlaceHolder.Controls.Add(uc);
}
//Save
protected void Save_Click(object sender, EventArgs e)
{
foreach (var control in AddVisaPlaceHolder.Controls)
{
var usercontrol = control as AddVisaUserControl;
//you can access properties from usercontrol
//Implement save logic here
}
}

Related

append tr when a value is not null using item template

I am appending rows using item template
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>
the problem with the above one is if someFunction(School.Name,"Name") returns null it appends empty tr, so i want to append tr only if someFunction(School.Name,"Name")!=null
So i tried the below code by adding a if condition but it is not compiling !!
It says school.name is not defined
<% if(someFunction(School.Name,"Name")!=null){
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>
} %>
You could take care of it from the code behind.
<td id="tdAppend" runat="server"> Content </td>
if(School.Name == null)
tdAppend.Visible = false;
UPDATED ANSWER:
currentItemIndex = -1;
listView.ItemDataBound += listView_ItemDataBound;
CheckQueryString();
//Hide or show the table header based on id
HtmlControl thControl = listView.FindControl("thAppend") as HtmlControl;
if (thControl != null)
{
if (obj.ID == 12)
{
thControl.Visible = true;
}
else
{
thControl.Visible = false;
for (int i = 0; i < listView.Items.Count; i++)
{
HtmlControl tdControl = listView.Items[i].FindControl("tdSchoolName") as HtmlControl;
if (tdControl != null)
{
tdControl.Visible = false;
}
}
}
}
For convenience:
void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
currentItemIndex++;
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
HtmlTableRow trControl = (HtmlTableRow)e.Item.FindControl("MainTableRow");
if (IsOdd(currentItemIndex))
trControl.BgColor = "DarkGray";
}
}
private bool IsOdd(int value)
{
return value % 2 != 0;
}
It's a pretty straight forward practice.. Handling the ListView.OnItemDataBinding event.
Markup
<%# Import Namespace="MyNamespace" %>
<asp:ListView ID="lview" runat="server" OnItemDataBound="lview_ItemDataBound" ItemPlaceholderID="Placeholder1">
<ItemTemplate>
<tr>
<td><%# ((School)Container.DataItem).Name %></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table>
<div id="Placeholder1" runat="server"></div>
</table>
</LayoutTemplate>
</asp:ListView>
Code-Behind
namespace MyNamespace
{
public class Default : Page
{
IEnumerable<School> dataItems {get;set;}
protected void Page_Load(object sender, EventArgs e)
{
dataItems = new School[] { new School() { Name = "School 1" }, new School() { Name = "School 2" }, new School() { Name = null }, new School() { Name = "School 3" } }.AsEnumerable();
lview.DataSource = dataItems;
lview.DataBind();
}
protected void lview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListView listview = (ListView)sender;
ListViewItem row = e.Item;
School dataItem = (School)e.Item.DataItem;
if (dataItem.Name == null)
{
row.Visible = false;
}
}
}
public class School
{
public string Name { get; set; }
}
}
personally, I don't usually use tables.. it's a fairly odd-looking layout template.

DropdownList SelectedIndexChanged inside Repeater fires up while under UpdatePanel and click another Dropdownlist outside repeater

I have a 2 Dropdownlist outside a repeater (1st ddl will trigger and refresh the content of the repeater, 2nd ddl refresh its list after an item inside the repeater were executed) and a dropdownlist control inside the repeater.i don't have a problem with the 1st dropdownlist but the 2nd dropdownlist when triggered via selectedindexchanged, it also fires up the selectedindexchanged of the dropdownlist inside the repeater. I put up an UpdatePanel on the repeater and on the 2nd dropdownlist but still the error occurs.
How can i trace the step by step execution and also why the selectedindexchanged handler fires up?
Here is the code snippet:
CODE BEHIND:
public partial class NewStyleItem : System.Web.UI.Page
{
protected StyleTemplateManager styleTempMngr = new StyleTemplateManager();
protected StyleFabricationManager styleFabMngr = new StyleFabricationManager();
protected List<ComponentReferenceView> ReferenceLookUpList
{
get
{
return (List<ComponentReferenceView>)ViewState["ReferenceLookUpView"];
}
set
{
ViewState["ReferenceLookUpView"] = value;
}
}
protected List<ComponentReferenceView> ReferenceViewList
{
get
{
return (List<ComponentReferenceView>)ViewState["CompReferenceView"];
}
set
{
ViewState["CompReferenceView"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
LoadBrandList();
ddlGarmentList.Enabled = false;
//ReferenceLookUpList = new List<ComponentReferenceView>();
}
else
{
}
//rptStyleComponents.DataSource = ReferenceLookUpList;
//rptStyleComponents.DataBind();
}
protected void LoadBrandList()
{
BrandManager brandMngr = new BrandManager();
ddlBrandList.Items.Clear();
ddlBrandList.Items.Add(new ListItem { Text = "Select brand", Value = "" });
foreach (Brand item in brandMngr.Brands())
{
ddlBrandList.Items.Add(new ListItem
{
Text = item.BrandDescription,
Value = item.BrandCode
});
}
}
protected void LoadComponentInfo(string BrandCode)
{
if (styleTempMngr.FindTemplateByBrandCode(BrandCode) == true)
{
ReferenceLookUpList = (from StyleComponent i in styleFabMngr.ComponentList
join StyleFabReference x in styleTempMngr.ReferenceList on
i.SectionCode equals x.SectionCode
select new ComponentReferenceView
{
InOrder = x.SortOrder,
IsNeeded = x.IsRequired,
SCode = x.SeriesCode,
SecCode = x.SectionCode,
SecName = i.SectionName,
EntryValue = x.DataCharacter
}).OrderBy(p => p.InOrder).ToList();
ReferenceLookUpList.Where(row => row.SecCode == "BCODE").ToList()
.ForEach(refItem => refItem.EntryValue = ddlBrandList.SelectedValue);
var DistinctList = (from StyleFabReference x in styleTempMngr.ReferenceList.OrderBy(p => p.SortOrder).ToList()
where x.SectionCode != "SPCHAR" && x.SectionCode != "BCODE"
select x.SectionCode).Distinct().ToList();
ReferenceViewList = (from StyleComponent i in styleFabMngr.ComponentList
join x in DistinctList on
i.SectionCode equals x
select new ComponentReferenceView
{
SecCode = x,
SecName = i.SectionName
}).OrderBy(p => p.InOrder).ToList();
}
else
ReferenceViewList = new List<ComponentReferenceView>();
rptStyleComponents.DataSource = ReferenceViewList;
rptStyleComponents.DataBind();
}
protected void LoadGarments(string ApparelCode)
{
if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == true)
{
ddlGarmentList.SelectedIndex = 0;
ddlGarmentList.Enabled = false;
}
else
{
ddlGarmentList.Enabled = true;
GarmentManager gmMngr = new GarmentManager();
List<Garment> garmentList = new List<Garment>();
garmentList = gmMngr.GetGarmentsByBrand(ddlBrandList.SelectedValue, ApparelCode);
ddlGarmentList.Items.Clear();
ddlGarmentList.Items.Add(new ListItem { Text = "Select garment", Value = "" });
foreach (Garment item in garmentList)
{
ddlGarmentList.Items.Add(new ListItem { Text = item.GarmentDescription, Value = item.GarmentCode });
}
}
}
protected void ddlBrandList_SelectedIndexChanged(object sender, EventArgs e)
{
txtNewStyleNo.Text = string.Empty;
if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == false)
LoadComponentInfo(ddlBrandList.SelectedValue);
}
[Serializable]
public class ComponentReferenceView
{
public string SCode
{
get;
set;
}
public string SecCode
{
get;
set;
}
public string SecName
{
get;
set;
}
public int InOrder
{
get;
set;
}
public bool IsNeeded
{
get;
set;
}
public string EntryValue
{
get;
set;
}
}
protected void rptStyleComponents_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
protected void rptStyleComponents_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlControl = (DropDownList)e.Item.FindControl("ddlDataSelection");
if (ddlControl != null)
{
ddlControl.AppendDataBoundItems = true;
ddlControl.Items.Add(new ListItem { Text = "Select " + ReferenceViewList[e.Item.ItemIndex].SecName.ToLowerInvariant(), Value = "" });
var itemlist = (from x in styleFabMngr.ComponentRowItems(ReferenceViewList[e.Item.ItemIndex].SecCode).AsEnumerable()
select new ListItem
{
Text = x.Field<string>("col_name"),
Value = x.Field<string>("col_value")
});
ddlControl.Items.AddRange(itemlist.ToArray<ListItem>());
ddlControl.EnableViewState = false;
ddlControl.AutoPostBack = true;
ddlControl.SelectedIndexChanged += new EventHandler(DropDownListSelectedIndexChanged);
}
}
}
protected virtual void DropDownListSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
RepeaterItem item = (RepeaterItem)ddl.NamingContainer;
ReferenceViewList[item.ItemIndex].EntryValue = ddl.SelectedValue;
ConstructStyleNo();
var Apparelfound = ReferenceViewList.Where(x => x.SecCode == "APCODE" &&
string.IsNullOrEmpty(x.EntryValue) == false).FirstOrDefault();
if (Apparelfound != null)
{
ddlGarmentList.Enabled = true;
LoadGarments(Apparelfound.EntryValue);
}
else
{
ddlGarmentList.Enabled = false;
}
}
private void ConstructStyleNo()
{
foreach (ComponentReferenceView item in ReferenceViewList)
{
ReferenceLookUpList.Where(row => row.SecCode == item.SecCode).ToList()
.ForEach(refItem => refItem.EntryValue = item.EntryValue);
}
string sResult = string.Empty;
var result = (from x in ReferenceLookUpList.OrderBy(y => y.InOrder)
select x.EntryValue);
txtNewStyleNo.Text = string.Empty;
if (result != null)
txtNewStyleNo.Text = String.Join("", result) + "0001";
}
private void ConstructStyleDescription()
{
txtStyleDescription.Text = ddlBrandList.SelectedItem.Text + "-" + ddlGarmentList.SelectedItem.Text;
}
protected void ddlGarmentList_SelectedIndexChanged(object sender, EventArgs e)
{
txtStyleDescription.Text = string.Empty;
if (string.IsNullOrEmpty(ddlGarmentList.SelectedValue) == false)
ConstructStyleDescription();
}
}
ASPX:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="upnlNewFabrication" runat="server">
<ContentTemplate>
<h1>
New Style Item
</h1>
<br />
<%--<usrCtrl:style_generation ID="stylegen_frm" runat="server">
</usrCtrl:style_generation>--%>
<table>
<tr>
<td>
Brand :
</td>
<td>
<asp:DropDownList ID="ddlBrandList" runat="server"
onselectedindexchanged="ddlBrandList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td>
Generated Style# :
</td>
<td>
<asp:TextBox ID="txtNewStyleNo" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Style Description :
</td>
<td>
<asp:TextBox ID="txtStyleDescription" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<asp:Repeater ID="rptStyleComponents" runat="server"
onitemdatabound="rptStyleComponents_ItemDataBound"
onitemcreated="rptStyleComponents_ItemCreated"
EnableViewState="True">
<ItemTemplate>
<asp:UpdatePanel ID="upnlComponent" runat="server">
<ContentTemplate>
<tr>
<td>
<asp:Label ID="lblSectionName" runat="server" Text='<%# Eval("SecName")%>' ></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlDataSelection" runat="server">
</asp:DropDownList>
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ddlDataSelection" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<table>
<tr>
<td colspan="2">Description Items</td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td>Garment Type:</td>
<td>
<asp:UpdatePanel ID="upGarment" runat="server" >
<ContentTemplate>
<asp:DropDownList ID="ddlGarmentList" runat="server"
AppendDataBoundItems="True" AutoPostBack="True"
onselectedindexchanged="ddlGarmentList_SelectedIndexChanged">
<asp:ListItem Text = "Select garment" Value = ""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sdsGarment" runat="server"
ConnectionString="<%$ ConnectionStrings:IRMSConnectionString %>"
SelectCommand="Select * from Garments"></asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ddlGarmentList" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>Cost Price:</td>
<td>
<asp:TextBox ID="txtCostPrice" runat="server"></asp:TextBox></td>
<td>SRP :</td>
<td><asp:TextBox ID="txtSRP" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Quantity :</td>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td>Week :</td>
<td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

all data disappears after PostBack

So on a textbox I will write how many dropdownlists, textboxes, label etc and the page ia AutoPostBack. But after I choose an item on a gridview, the page ia autopostback and all the data dissapear. I want gridview to be AutoPostBack but how can I make it not to dissapear all the other item I have added from the first textbox?
For more info here is the source code:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 248px;
}
.auto-style2 {
width: 253px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<table style="width:100%;">
<tr>
<td class="auto-style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder4" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
<asp:PlaceHolder ID="PlaceHolder5" runat="server"></asp:PlaceHolder>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style2">
</td>
<td> </td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>
and Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
DropDownList artikulli;
TextBox cmimi;
Label tregoCmimi;
TextBox sasia;
Label cmimiGjithsej;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
int a = int.Parse(TextBox1.Text);
for (int j = 1; j <= a; j++)
{
Guid IDUnik = new Guid();
artikulli = new DropDownList();
cmimi = new TextBox();
tregoCmimi = new Label();
sasia = new TextBox();
cmimiGjithsej = new Label();
artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
artikulli.AutoPostBack = true;
cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;
PlaceHolder1.Controls.Add(artikulli);
PlaceHolder2.Controls.Add(cmimi);
PlaceHolder3.Controls.Add(tregoCmimi);
PlaceHolder4.Controls.Add(sasia);
PlaceHolder5.Controls.Add(cmimiGjithsej);
artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
}
}
}
}
you can do as below
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
AddItems();
}
}
private void AddItems()
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
int a = int.Parse(TextBox1.Text);
for (int j = 1; j <= a; j++)
{
Guid IDUnik = new Guid();
artikulli = new DropDownList();
cmimi = new TextBox();
tregoCmimi = new Label();
sasia = new TextBox();
cmimiGjithsej = new Label();
artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
artikulli.AutoPostBack = true;
cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;
PlaceHolder1.Controls.Add(artikulli);
PlaceHolder2.Controls.Add(cmimi);
PlaceHolder3.Controls.Add(tregoCmimi);
PlaceHolder4.Controls.Add(sasia);
PlaceHolder5.Controls.Add(cmimiGjithsej);
artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
}
}
}
you don't need OnTextChanged="TextBox1_TextChanged" in your textbox
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Populate();
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Populate();
}
void Populate()
{
PlaceHolder1.Controls.Clear();
PlaceHolder2.Controls.Clear();
PlaceHolder3.Controls.Clear();
PlaceHolder4.Controls.Clear();
PlaceHolder5.Controls.Clear();
if (!string.IsNullOrEmpty(TextBox1.Text))
{
int a = int.Parse(TextBox1.Text);
for (int j = 1; j <= a; j++)
{
Guid IDUnik = new Guid();
artikulli = new DropDownList();
cmimi = new TextBox();
tregoCmimi = new Label();
sasia = new TextBox();
cmimiGjithsej = new Label();
artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));
artikulli.AutoPostBack = true;
cmimi.ID = j.ToString(IDUnik.ToString("D").Substring(30));
tregoCmimi.ID = j.ToString(IDUnik.ToString("P"));
sasia.ID = j.ToString(j.ToString(IDUnik.ToString("X")));
cmimiGjithsej.ID = j.ToString(j.ToString(IDUnik.ToString("B"))); ;
PlaceHolder1.Controls.Add(artikulli);
PlaceHolder2.Controls.Add(cmimi);
PlaceHolder3.Controls.Add(tregoCmimi);
PlaceHolder4.Controls.Add(sasia);
PlaceHolder5.Controls.Add(cmimiGjithsej);
artikulli.Items.Insert(0, new ListItem("<Select Subject>", "0"));
artikulli.Items.Insert(1, new ListItem("<Select Subject>", "1"));
}
}
}

add a onclientclick to gridview pager

Using asp.net and the gridview control with the pager.
How do I add a onclientclick to it, So that it will only page once the onclientclick returns true.
Is this possible as it can be done with a link?
i know I can do this
GridView pager page buttons accessibility
myGridView.Attributes.Add("onClick", "setOKToClose();")
but it addes it to the grid and I want it only added to the pager
Thanks
Convert your pager to template but you will have to make your own logic for showing and managing page index.
Here in a sample code
<PagerTemplate>
<asp:Panel ID="gvpanel" runat="server">
<table cellpadding="3" cellspacing="3" class="style4">
<tr>
<td width="20%">
<asp:Label ID="Label4" runat="server" CssClass="lb" Text="Page "></asp:Label>
<asp:DropDownList ID="ddpage" runat="server" AutoPostBack="True" CssClass="txt"
onselectedindexchanged="ddpage_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="Label5" runat="server" CssClass="lb" Text="of "></asp:Label>
<asp:Label ID="lbltot" runat="server" CssClass="lb" Text="0"></asp:Label>
</td>
<td width="60%">
<table class="style1">
<tr>
<td width="15%">
<asp:LinkButton ID="lbp10" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lbp10_Click">Previous 10</asp:LinkButton>
</td>
<td width="15%">
<asp:LinkButton ID="lbp" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lbp_Click">Previous</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb1" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">1 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb2" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">2 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb3" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">3 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb4" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">4 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb5" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">5 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb6" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">6 </asp:LinkButton>
</td>
<td >
<asp:LinkButton ID="lb7" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">7 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb8" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">8 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb9" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">9 </asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lb10" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lb1_Click">10</asp:LinkButton>
</td>
<td width="15%">
<asp:LinkButton ID="lbn" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lbn_Click">Next</asp:LinkButton>
</td>
<td width="15%">
<asp:LinkButton ID="lbn10" runat="server" CssClass="smalllinkbuttonf"
Enabled="False" onclick="lbn10_Click">Next 10</asp:LinkButton>
</td>
</tr>
</table>
</td>
<td width="20%">
</td>
</tr>
</table>
</asp:Panel>
</PagerTemplate>
Here is the c# code
protected void lbp10_Click(object sender, EventArgs e)
{
GridView1.PageIndex = (GridView1.PageIndex - 10);
bind();
}
protected void lbp_Click(object sender, EventArgs e)
{
GridView1.PageIndex = (GridView1.PageIndex - 1);
bind();
}
protected void lbn_Click(object sender, EventArgs e)
{
GridView1.PageIndex = (GridView1.PageIndex + 1);
bind();
}
protected void lbn10_Click(object sender, EventArgs e)
{
GridView1.PageIndex = (GridView1.PageIndex + 10);
bind();
}
protected void lb1_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridView1.PageIndex = (int.Parse(lb.Text) - 1);
bind();
}
Code for creating the footer
void addfooter(int rcount)
{
if (GridView1.BottomPagerRow == null)
{
return;
}
Panel p = (Panel)GridView1.BottomPagerRow.FindControl("gvpanel");
DropDownList dd = (DropDownList)p.FindControl("ddpage");
for (int i = 0; i < rcount; i++)
{
ListItem li = new ListItem();
li.Text = (i + 1).ToString();
li.Value = (i + 1).ToString();
dd.Items.Add(li);
}
Label lbltot = (Label)p.FindControl("lbltot");
lbltot.Text = GridView1.PageCount.ToString();
dd.SelectedIndex = GridView1.PageIndex;
LinkButton lbp10 = (LinkButton)p.FindControl("lbp10");
lbp10.Enabled = false;
lbp10.CssClass = "smalllinkbuttonfd";
LinkButton lbp = (LinkButton)p.FindControl("lbp");
lbp.Enabled = false;
lbp.CssClass = "smalllinkbuttonfd";
LinkButton lbn10 = (LinkButton)p.FindControl("lbn10");
lbn10.Enabled = false;
LinkButton lbn = (LinkButton)p.FindControl("lbn");
lbn.CssClass = "smalllinkbuttonfd";
lbn.Enabled = false;
lbn10.CssClass = "smalllinkbuttonfd";
int cpage = GridView1.PageIndex + 1;
int totpage = GridView1.PageCount;
if (cpage > 10)
{
lbp10.Enabled = true;
lbp10.CssClass = "smalllinkbuttonf";
}
if (cpage > 1)
{
lbp.Enabled = true;
lbp.CssClass = "smalllinkbuttonf";
}
if (cpage < totpage)
{
lbn.Enabled = true;
lbn.CssClass = "smalllinkbuttonf";
}
if (cpage + 10 < totpage)
{
lbn10.Enabled = true;
lbn10.CssClass = "smalllinkbuttonf";
}
for (int i = 1; i < 11; i++)
{
LinkButton lb = (LinkButton)p.FindControl("lb" + i.ToString());
lb.Enabled = false;
lb.CssClass = "smalllinkbuttonfd";
}
int tstart = cpage / 10;
int lcount = tstart + 10;
//int scount = tstart - 10;
int cnt = 1;
for (int i = (tstart * 10); i <= ((tstart * 10) + 10); i++)
{
if (cnt > 10)
{
return;
} if ((i + 1) > totpage)
{
LinkButton lb = (LinkButton)p.FindControl("lb" + cnt.ToString());
lb.Enabled = false;
lb.Text = (i + 1).ToString();
lb.CssClass = "smalllinkbuttonfd";
cnt++;
}
else
{
if ((i + 1) == cpage)
{
LinkButton lb = (LinkButton)p.FindControl("lb" + cnt.ToString());
lb.Enabled = false;
lb.Text = (i + 1).ToString();
lb.CssClass = "smalllinkbuttonfd";
cnt++;
}
else
{
LinkButton lb = (LinkButton)p.FindControl("lb" + cnt.ToString());
lb.Enabled = true;
lb.Text = (i + 1).ToString();
lb.CssClass = "smalllinkbuttonf";
cnt++;
}
}
}
}
Onlink button now you can add onclientclick
This link has a correct answer combined with this answer.
Be sure to add a <asp:ScriptManager runat="server" /> tag in your document. Add the javascript in the page header section.
function pageLoad(sender, args) {
//Your jquery code
}
Excellent reference can be found in $(document).ready() and pageLoad() are not the same!
Cheers!
add to GridView RowDataBound
If e.Row.RowType = DataControlRowType.Pager Then
e.Row.Attributes.Add("onclick", "js_function()")
End If

Copying items from one <asp:ListBox> to another dynamically

This is our code:
<div>
<table style="width: 349px">
<tr>
<td class="style1">
<asp:ListBox ID="leftbox" runat="server" Height="114px" Width="212px"
SelectionMode="Multiple" AutoPostBack="True">
</asp:ListBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text=">>" onclick="Button1_Click" /><br />
<asp:Button ID="Button2" runat="server" Text="<<" onclick="Button2_Click" />
</td>
<td>
<asp:ListBox ID="rightbox" runat="server" Height="117px" Width="231px"
SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
</td>
</tr>
</table>
</div>
The code behind is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> list= GetNameList();
leftbox.DataSource = list;
leftbox.DataBind();
}
}
private static List<string> GetNameList()
{
List<string> list = new List<string>();
list.Add("keerthana");
list.Add("sirisha");
list.Add("anusha");
list.Add("Anuradha");
list.Add("Bhavani");
list.Add("divya");
list.Sort();
return list;
}
Please tell me how to add two or more selected items one by one from leftbox to rightbox, without using server-side code (as below):
protected void Button1_Click(object sender, EventArgs e)
{
if (leftbox.SelectedIndex != -1)
{
rightbox.Items.Add(leftbox.SelectedItem.Text);
leftbox.Items.Remove(leftbox.SelectedItem.Text);
}
}
Thanks in advance...
Use client side javascript to do this, so you don't postback every time someone moves something, if you have jquery it can be as easy as:
$("#Button1").button().click(function() {
$("#leftbox option:selected").each(function() {
$("#rightbox").append(this);
});
});
Here is Linq way of get selected items form ListBox
var selectedItems = leftbox.Items.OfType<ListItem>().Where(item => item.Selected).ToList();
then you can add all selected values to rightbox and finally remove from leftbox
EDIT
protected void Button1_Click(object sender, EventArgs e)
{
List<ListItem> selectedItems = new List<ListItem>();
if (leftbox.SelectedIndex >= 0)
{
for (int i = 0; i < leftbox.Items.Count; i++)
{
if (leftbox.Items[i].Selected)
{
selectedItems.Add(leftbox.Items[i]);
}
}
}
for (int i = 0; i < selectedItems.Count; i++)
{
if (!rightbox.Items.Contains(selectedItems[i]))
rightbox.Items.Add(selectedItems[i]);
leftbox.Items.Remove(selectedItems[i]);
}
}
this will do the work
but the problem here is..
after you transfer the item and click save button
it will submit first before your save code will read
all the item you move to Leftbox from Rightbox will go back to Rightbox viseversa
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#left").bind("click", function () {
var options = $("[id*=RIghtBox] option:selected");
for (var i = 0; i < options.length; i++) {
var opt = $(options[i]).clone();
$(options[i]).remove();
$("[id*=LeftBox]").append(opt);
}
});
$("#right").bind("click", function () {
var options = $("[id*=LeftBox] option:selected");
for (var i = 0; i < options.length; i++) {
var opt = $(options[i]).clone();
$(options[i]).remove();
$("[id*=RIghtBox]").append(opt);
}
});
});
</script>

Categories

Resources