Want to populate the second dropdown values based on selection first of first drop down.
id Bankname Branchname
1 HDFC AMT
2 HDFC gag
3 icic AMsfsaT
4 bpjn daD
in the first drop down if I select the HDFC then in second dropdown AMT,gag should come.
I am using following code to fill first dropdown.
string com = "Select * from mst_bank";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlbank.DataSource = dt;
ddlbank.DataBind();
ddlbank.DataTextField = "Bankname";
ddlbank.DataValueField = "ID";
Add 2 DropDownLists:
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" /><br />
<asp:DropDownList ID="ddl2" runat="server" />
And your code behind will be:
private string conStr = WebConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDDL1();
FillDDL2(ddl1.SelectedValue);
}
}
protected void FillDDL1()
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("Select * from mst_bank", con);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddl1.DataSource = ds;
ddl1.DataTextField = "Bankname";
ddl1.DataValueField = "ID";
ddl1.DataBind();
}
catch (Exception ex)
{
//work with exception
}
finally
{
con.Close();
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
FillDDL2(ddl1.SelectedValue);
}
protected void FillDDL2(string id)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("Select * from second_table where BANK_ID = #id", con);
cmd.Parameters.Add("#id", SqlDbType.Int).Value = id;
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddl1.DataSource = ds;
ddl1.DataTextField = "Name";
ddl1.DataValueField = "ID";
ddl1.DataBind();
}
catch (Exception ex)
{
//work with exception
}
finally
{
con.Close();
}
}
On PostBack we fill dropdown1 and dropdown2, using corresponding value from the dropdown1. Method ddl1_SelectedIndexChanged will be occur when user changes selection, and dropdown2 will be filled with a corresponding values. Of course, you have to change a SQL-query there.
Related
asp .net 4.0 Use bootstrap-select When running the code behind, the dropdown list is not displayed. But If not running the code behind, the dropdown list is displayed.
page.aspx
<asp:DropDownList ID="ddl" runat="server" CssClass="selectpicker" OnSelectedIndexChange="ddlChange" AutoPostBack="true">
</asp:DropDownList>
page.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ddl.Style["display"] = "inline";
SqlConnection sqlCon = new SqlConnection(ConnectString);
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("select name from customer", sqlCon);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddl.DataSource = dt;
ddl.DataValueField = "name";
ddl.DataTextField = "name";
ddl.DataBind();
sqlCon.Close();
}
How do I fix it?
Thank you.
Ok. Seeing from your comment you could use below:
if(dt.Rows.Count>0)
{
ddl.Visible= true;
}
else
{
ddl.Visible= false;
}
protected void Page_Load(object sender, EventArgs e) {
ddl.Style["display"] = "inline";
SqlConnection sqlCon = new SqlConnection(ConnectString);
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("select name from customer", sqlCon);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddl.DataSource = dt;
ddl.DataValueField = "name";
ddl.DataTextField = "name";
ddl.DataBind();
sqlCon.Close();
}
This is my one tag:
<asp:Button ID="button" runat="server" Text="ShowOrder" onclick="newTab" />
This is my 'aspx.cs' which will be called when button is clicked
protected void newTab(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?id="+txtSearchCustomerByID.Value);
}
What I want is to print my sql table on loaded web tab (new page) when it gets loaded.
My stored procedure is displaying the data of my table where id is equal to "id entered by user in textbox".
Now,
protected void Page_Load(object sender, EventArgs e)
{
int id_no = int.Parse(Request.QueryString["id"]);
if (Page.IsPostBack)
{
showOrders(id_no);
}
}
Now what should I have in 'Default2.aspx' so that I will get my table by using,
public void showOrders(int id)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
SqlCommand cmd = new SqlCommand("showOrdersSP", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#id", id);
con.Open();
.......
}
}
I need to use DataTable
So simply when I clicked, I will get data table on new page
You can refer below code:
public void showOrders(int id)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
DataTable dt = new DataTable();
SqlParameter[] p1 = new SqlParameter[1];
p1[0] = new SqlParameter("#id", id);
dt= getRecords_table("showOrdersSP", p1); // you will get your DataTable here
}
}
// Common Method for FillYour Tables
private DataTable getRecords_table(string spname, SqlParameter[] para)
{
string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["connName"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(spname, con);
ad.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
ad.SelectCommand.Parameters.AddRange(para);
con.Open();
ad.Fill(dt);
con.Close();
return dt;
}
Hope it will helps you
Thanks
i have gridview which populate date from database i want to change seclected data on dropdown SelectedIndexChanged when i select the first index it selected data from database when i chang selections it get another data try this code but nosense this is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
if (ddlTguidedit.SelectedIndex==0)
{
string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
else
{
string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
protected void ddlTguidedit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlTguidedit.SelectedIndex == 0)
{
string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
else
{
string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
}
why it doesnt work ??
Remove BindData() from !IsPostBack(). You are loading the grid data on DropDownList SelectedIndexChanged. There is no need of BindData() function to be in !IsPostBack(). BindData() function always loads no matter at what index the DropDownList is, it will always take the index as 0.
i am adding multiple checkboxes in my asp.net page by doing this:
public static CheckBox[] chck;
on pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select count(CompanyName) from Stock_Company");
cmd.Connection = con;
comno = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
chck = new CheckBox[comno];
}
}
now i have a function which is generating the checkboxes :
public void generatecheckbox1()
{
con.Open();
SqlCommand cmd = new SqlCommand("select CompanyName from Stock_Company");
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
chck[i] = new CheckBox();
chck[i].ID = "chck" + Convert.ToString(i);
chck[i].Text = dt.Rows[i]["CompanyName"].ToString();
pnlcom1.Controls.Add(chck[i]);
pnlcom1.Controls.Add(new LiteralControl("<br />"));
}
}
and i am calling this on a combobox event:
protected void ddluserwebser_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddluserwebser.SelectedItem.Text == "Custom")
{
generatecheckbox1();
}
}
as far as this all are working fine ... but in a button click i want to get the select checkbox's text which i am not getting
i made a function :
public string getbsecompany()
{
string companyname = "";
string bsetricker = "";
con.Open();
SqlCommand cmd = new SqlCommand("select CompanyName from Stock_Company");
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (chck[i].Checked == true) **THE PROBLEM IS HERE**
{
companyname = chck[i].Text;
con.Open();
SqlCommand cmdd = new SqlCommand("select BSETickerCode from Stock_Company where CompanyName='" + companyname + "'");
cmdd.Connection = con;
bsetricker += bsetricker + "+" + cmdd.ExecuteScalar();
con.Close();
}
}
return bsetricker;
}
and i am calling it here:
protected void btnusersave_Click(object sender, EventArgs e)
{
string bsetricker = "";
bsetricker = getbsecompany();
}
the problem is i am not getting the checked box's text. when i am checking if (chck[i].Checked == true) i am gettin false and all checkboxes are checked.
What should i do now?
any help
The dynamic controls should added to page in On_Init() for each time if you want it display in page.
Else there's nothing you can get.
Plus, better not use a static value to contains checkBox List, it will cause problem when multi user access same page. You can save them in session or try this.Form.FindControls()
How to fire ActiveTabChanged for TabContainer. I have written a code to populate the TabPanel HeaderText from the database and this is my code
public partial class dynamicTab : System.Web.UI.Page
{
string strCon = ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString.ToString();
TabContainer ajxTab;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// createTab();
pnlDynamic.Controls.Add(ajxTab);
ajxTab_ActiveTabChanged(ajxTab, EventArgs.Empty);
}
}
private void ajxTab_ActiveTabChanged(TabContainer ajxTab, EventArgs eventArgs)
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select * from Employee where DeptID='" + ajxTab.ActiveTab.ID.ToString() + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView grd = new GridView();
grd.AutoGenerateColumns = true;
grd.DataSource = ds;
grd.DataBind();
pnlDynamic.Controls.Add(grd);
}
protected void page_init(object sender, eventargs e)
{
createtab();
}
private void createTab()
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select DeptID,DepartmentName from Department", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
ajxTab = new AjaxControlToolkit.TabContainer();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TabPanel pnl = new TabPanel();
pnl.HeaderText = ds.Tables[0].Rows[i]["DepartmentName"].ToString();
pnl.ID = ds.Tables[0].Rows[i]["DeptID"].ToString();
ajxTab.Tabs.Add(pnl);
ajxTab.ActiveTabIndex = 0;
}
}
}
For the first time ActiveTabChanged fired perfectly , but when I click on the second tab ActiveTabChanged is not getting fired. I tried by setting ajxTab.AutoPostBack=true but the tab container is not getting visible when the event occurs.
This is my sample output
On clicking computers I would like to load grid with those details so can some one help me
You can bind the content of each tab in the OnActiveTabChanged event
OnActiveTabChanged="TabContainer1_ActiveTabChanged"
AutoPostBack="true"