I have 3 dropdown lists:
<tr>
<td align="left" class="style2">
Emp Code</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcode" runat="server" AutoPostBack="True"
onselectedindexchanged="ddcode_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcode" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
Company</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcompany" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcompany" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
Branch</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddbranch" runat="server"
onselectedindexchanged="ddbranch_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddbranch" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate> </asp:UpdatePanel>
</td>
</tr>
I also have a dropdown class:
public class Dropdown
{
Connection con = new Connection();
public void dropdwnlist(string qry, DropDownList ddl)
{
con.gettable(qry);
if (con.dt.Rows.Count > 0)
{
if (con.dt.Columns.Count == 2)
{
string str1 = con.dt.Columns[0].ColumnName.ToString();
string str2 = con.dt.Columns[1].ColumnName.ToString();
ddl.DataValueField = str1;
ddl.DataTextField = str2;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str1);
con.dt.Columns.Remove(str2);
}
else
{
string str = con.dt.Columns[0].ColumnName.ToString();
ddl.DataValueField = str;
ddl.DataTextField = str;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str);
//con.dt.Columns.Remove(str2);
}
}
ddl.Items.Insert(0, ("--Select--"));
}
My button click is:
Dropdown dwn = new Dropdown();
string str = "";
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillempcode();
//fillcompany();
//filldepartment();
}
}
protected void fillempcode()
{
string str = "select MachID,EmpCode from EmpDetails where StatusID='0'";
dwn.dropdwnlist(str, ddcode);
}
protected void fillcompany()
{
//Where EmpCode='" + ddcode.SelectedItem.Text + "'
str = "select CompanyID,CompanyName from Company ";
dwn.dropdwnlist(str, ddcompany);
str = "select CompanyID from View_Company where EmpCode='" + ddcode.SelectedItem.Text + "'";
dr=conn.query(str);
if (dr.Read())
{
string id = dr[0].ToString();
ddcompany.SelectedItem.Text = id;
}
}
protected void fillbranch()
{
//where EmpCode='" + ddcode.SelectedItem.Text + "'
string str = "select BranchID,BranchName from View_Branch";
dwn.dropdwnlist(str, ddbranch);
}
My problem is I want to enable all dropdownllist when I selected on EmpCode dropdown list. now it works fine but Company and Branch dropdown shows same value twise. may I know the reason?
Try rewriting the dropdwnlist method like this,
public void dropdwnlist(string qry, DropDownList ddl)
{
ddl.Items.Clear();
con.gettable(qry);
if (con.dt.Rows.Count > 0)
{
if (con.dt.Columns.Count == 2)
{
string str1 = con.dt.Columns[0].ColumnName.ToString();
string str2 = con.dt.Columns[1].ColumnName.ToString();
ddl.DataValueField = str1;
ddl.DataTextField = str2;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str1);
con.dt.Columns.Remove(str2);
}
else
{
string str = con.dt.Columns[0].ColumnName.ToString();
ddl.DataValueField = str;
ddl.DataTextField = str;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str);
//con.dt.Columns.Remove(str2);
}
}
ddl.Items.Insert(0, ("--Select--"));
ddl.SelectedIndex = 0;
}
Hope this helps...
Related
I am developing an application (Quiz Engine) in Asp.Net with C#. I have a database table (Q_ID,Question,option1,option2,option3,answer).
What I want is, to retrieve these data and display to the user as Question and Answer and enable the user to select with the Radio button. My problem is that how I can display this Question and options in radiobuttonlist.
my asp page is
<asp:DataList ID="DataList2" runat="server" RepeatDirection="Vertical">
<ItemTemplate>
<br />
<table class="auto-style1">
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList2" runat="server">
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
and my cod behind is like
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
DataTable dt = new DataTable();
string connStr = #"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
using (MySqlConnection cn = new MySqlConnection(connStr))
{
MySqlDataAdapter adp = new MySqlDataAdapter("select LO_id,Q_Id,level,Question,option1,option2 from quiz WHERE LO_id='LO111'", cn);
adp.Fill(dt);
}
if (dt.Rows.Count > 0)
{
DataList2.DataSource = dt;
DataList2.DataBind();
}
foreach (DataListItem dli in DataList2.Items)
{
RadioButtonList RadioButtonList2 = (RadioButtonList)DataList2.FindControl("RadioButtonList2");
//RadioButtonList2.Items.Clear();
RadioButtonList2.Items.Insert(0, new ListItem("option1".ToString(), "option1".ToString()));
RadioButtonList2.Items.Insert(1, new ListItem("option2".ToString(), "option2".ToString()));
}
}
I am using repeater control and showing paging '<< Previous next >>' it is working fine.
But i want to show links (rolling numbers) in between like below:
Previous << 1 2 3 4 5 next >>
have a look this picture for idea.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" style="border-color: #336699;" cellspacing="0">
<tr style="background-color:#336699; color:White;">
<td>Product ID</td>
<td>Product Name</td>
<td>Unit Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductID") %></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div> <table width="500px" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="25%"> <asp:Label ID="Label2" runat="server" />
</td>
<td width="25%"><asp:HyperLink ID="linkPrev" runat="server">« Previous</asp:HyperLink>
</td>
<td width="25%"><asp:HyperLink ID="linkNext" runat="server">Next »</asp:HyperLink></td>
<td width="25%"><asp:Label ID="Label1" runat="server" /> </td>
</tr>
</table> </div>
c#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Products", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
con.Close();
DataTable table = new DataTable();
da.Fill(table);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
int currentPage;
if (Request.QueryString["page"] != null)
{
currentPage = Int32.Parse(Request.QueryString["page"]);
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
Label2.Text = "Results" + pds.DataSourceCount;
if (!pds.IsFirstPage)
{
linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);
}
if (pds.IsFirstPage)
{
linkPrev.Visible = false;
linkPrev.Style["display"] = "block";
}
if (!pds.IsLastPage)
{
linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);
}
if (pds.IsLastPage)
{
linkNext.Visible = false;
linkPrev.Style["display"] = "block";
}
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
}
I bind the values in usercontrol dropdownlist
But when I add the usercontrol row that time values are not binded in dropdownlist
Code:
.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TimesheetUserControl.ascx.cs" Inherits="Portal.TimesheetUserControl" %>
<table>
<tr>
<td>
<asp:DropDownList ID="DropDownActivities" Width="150px" runat="server"></asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
.aspx
<uc:Timesheet ID="Timesheet" runat="server" />
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
BindActivities();
}
}
protected void BindActivities()
{
DropDownList dropActivities = Timesheet.FindControl("DropDownActivities") as DropDownList;
DbConnection.Open();
OleDbCommand cmd1 = new OleDbCommand("select designation from emp_master where username = '" + username + "'", DbConnection);
OleDbDataAdapter da = new OleDbDataAdapter(Deptcmd);
DataSet ds = new DataSet();
da.Fill(ds);
// DbConnection.Close();
dropActivities.DataSource = ds;
dropActivities.DataTextField = "ActivityName";
dropActivities.DataBind();
dropActivities.Items.Insert(0, new ListItem("--Select--", "0"));
}
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);
if (IsPostBack)
{
GenerateControls();
}
}
private void GenerateControls()
{
foreach (string i in NoOfControls)
{
TimesheetUserControl ctrl = (TimesheetUserControl)Page.LoadControl("TimesheetUserControl.ascx");
ctrl.ID = i;
this.rpt1.Controls.Add(ctrl);
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
Button thisButton = (Button)sender;
List<string> temp = null;
var uc = (TimesheetUserControl)this.LoadControl(#"TimesheetUserControl.ascx");
string id = Guid.NewGuid().ToString();
uc.ID = id;
temp = NoOfControls;
temp.Add(id);
NoOfControls = temp;
rpt1.Controls.Add(uc);
}
In the below image if Click add button rows are added but in second values are not binded in dropdownlist
Any ideas? Thanks in advance.
find the user controls by the id you given and then you can find the DropDownList inside the usercontrol.
protected void BindActivities()
{
foreach (string controlName in NoOfControls)
{
TimesheetUserControl userControl = Timesheet.FindControl(controlName) as TimesheetUserControl;
if(userControl == null) return;
DropDownList dropActivities = userControl.FindControl("DropDownActivities") as DropDownList;
if(dropActivities == null) return;
DbConnection.Open();
OleDbCommand cmd1 = new OleDbCommand("select designation from emp_master where username = '" + username + "'", DbConnection);
OleDbDataAdapter da = new OleDbDataAdapter(Deptcmd);
DataSet ds = new DataSet();
da.Fill(ds);
// DbConnection.Close();
dropActivities.DataSource = ds;
dropActivities.DataTextField = "ActivityName";
dropActivities.DataBind();
dropActivities.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
This is my aspx code...
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
Width="88%" Height="100px">
<ItemTemplate>
<table width="0" border="0" cellpadding="0" cellspacing="1">
<tr>
<td>
<%#Eval("Coupon_Image")%>'
</td>
</tr>
<tr>
<td height="20px">
</td>
</tr>
<tr>
<td align="center">
<%--<asp:LinkButton ID="Button1" runat="server" Width="145" Height="34" border="0" CommandArgument='<%# Eval("Coupon_Id") %>'
CommandName="Get Coupoun" OnClick="getCoupon_Click"></asp:LinkButton>--%>
<asp:Button ID="getCoupon" runat="server" Width="145" Height="34" border="0" CommandArgument='<%# Eval("Coupon_Id")%>' Text="Get Coupon"
OnClick="getCoupon_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
my code in code behind:
private void Binddata()
{
DataTable dtReport = new DataTable();
DataColumn Coupon_Id = new DataColumn("Coupon_Id");
DataColumn Coupon_Image = new DataColumn("Coupon_Image");
dtReport.Columns.Add(Coupon_Id);
dtReport.Columns.Add(Coupon_Image);
string sendquery = "select Coupon_Id,Coupon_Image from Admin where Coupon_Status='Active'";
SqlDataAdapter da = new SqlDataAdapter(sendquery, connection);
DataSet dsProfile = new DataSet();
da.Fill(dsProfile);
for (int i = 0; i < dsProfile.Tables[0].Rows.Count; i++)
{
DataRow row = dtReport.NewRow();
row["Coupon_Id"] = dsProfile.Tables[0].Rows[i]["Coupon_Id"].ToString();
string id = dsProfile.Tables[0].Rows[i]["Coupon_Id"].ToString();
string ImgName = dsProfile.Tables[0].Rows[i]["Coupon_Image"].ToString();
string Path = "http://localhost:2872/arpita-coupons/Coupon_Image/" + ImgName ;
row["Coupon_Image"] = "<img src='" + Path + "' height='200px' width='200px' />";
hddnpath.Value = Path + ImgName + id;
dtReport.Rows.Add(row);
}
DataList1.DataSource = dtReport;
DataList1.DataBind();
}
protected void getCoupon_Click(object sender, EventArgs e)
{
string id = hddnpath.Value;
string imagename = hddnpath.Value;
Response.Redirect("deals-forms.aspx?id="+id+" & ImgName="+imagename+" "+"");
}
my question is if i click the button to get the particular image to be redirect to the another page . but i am getting only the different image to the another aspx page...
You set id and imagename to the same value, then build a link from both. I guess there is the problem somewhere:
string id = hddnpath.Value;
string imagename = hddnpath.Value;
Response.Redirect("deals-forms.aspx?id=" + id // <-- this variable
+ " & ImgName=" + imagename // <-- and this one both
+ " " + ""); // contain hddnpath.Value
I want to assign same datasource to multiple DropDownLists in a form, when the page loads, only the first drop down list has filled the content of datasource, others are empty, what's the missing point? thanks for answers. Here are the codes;
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="panel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div1" align="center">
<table>
<tr>
<td><b>Brand-Model</b></td>
<td><asp:TextBox ID="brandText" runat="server" BorderStyle="Inset"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="brandText" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><b>Black</b></td>
<td><asp:DropDownList ID="blackList" runat="server"></asp:DropDownList></td>
<td><asp:HyperLink ID="HyperLink1" runat="server" Text="Add Cartridge" NavigateUrl="~/Admin/addCartridge.aspx"></asp:HyperLink></td>
</tr>
<tr>
<td><b>Color1</b></td>
<td><asp:DropDownList ID="colorList1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color2</b></td>
<td><asp:DropDownList ID="colorList2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color3</b></td>
<td><asp:DropDownList ID="colorList3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other1</b></td>
<td><asp:DropDownList ID="otherColor1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other2</b></td>
<td><asp:DropDownList ID="otherColor2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other3</b></td>
<td><asp:DropDownList ID="otherColor3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_OnClick" /></td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="submitInfo" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="submit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</form>
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
blackList.DataValueField = "name";
blackList.DataSource = reader;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = reader;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = reader;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = reader;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = reader;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = reader;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
Its becasue you are using dataReader instead of this use datatable will work for you. Reader is readonly and forward only thats why only first dropdonw get filled with data and others are empty.
changed code :
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader );
blackList.DataValueField = "name";
blackList.DataSource = dt ;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = dt ;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = dt ;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = dt ;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = dt ;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = dt ;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
According to the documentation data reader is forward only. Use data table or read into a list and bind your combos to it.
for (int i = 1; i <= 260; i++)
{
ContentPlaceHolder maincontent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
DropDownList a = (DropDownList)maincontent.FindControl("ddl" + i);
a.DataSource = ds;
a.DataTextField = "Options";
a.DataValueField = "id";
a.DataBind();
a.Items.Insert(0, new ListItem("Select", "0", true));
}