asp bootstrap-select not working - asp.net

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();
}

Related

System.Data.DataRowView in DropDownList instance of actual value

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["statesid"].ConnectionString);
public DataTable bindstate()
{
con.Open();
SqlCommand cmd = new SqlCommand("bindstateid",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindstateid();
}
}
public void bindstateid()
{
dal ds = new dal();
DataTable dt = new DataTable();
dt = ds.bindstate();
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
GridView1.DataSource = dt;
GridView1.DataBind();
}
I am getting system.data.datarowview in the dropdownlist instance of the actual value
You do not specify the DataTextField and DataValueField properties. These should be the names of a column in the DataTable.
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Column1";
DropDownList1.DataValueField = "Column2";
DropDownList1.DataBind();

ASP.NET C#,Javascript

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.

lable is not showing any output for the dropdown list selection

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LibraryConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlbookavail.Items.Add(new ListItem("select", "-1"));
ddlbookavail.AppendDataBoundItems = true;
SqlCommand cmd = new SqlCommand("select * from tblbookinfo", con);
con.Open();
ddlbookavail.DataSource = cmd.ExecuteReader();
ddlbookavail.DataTextField = "Name";
ddlbookavail.DataValueField = "Id";
ddlbookavail.DataBind();
con.Close();
txtdate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
}
in my design page i put the sql datasource from data and got this connection srting

populate gridview on dropdownlist indexchanged

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.

Making code duplication to refresh the gridview in asp.net

I am trying to update the gridview after updating some data. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = University.GetConnectionString();
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
con.Close();
}
protected void studentLevel_RowCommand(object sender, GridViewCommandEventArgs e)
{
int row = -1;
int.TryParse(e.CommandArgument as string, out row);
GridViewRow gdrow = studentLevel.Rows[row];
DataRow dr = ((DataTable)studentLevel.DataSource).Rows[gdrow.DataItemIndex];
string id = dr["ID"].ToString();
Student student = new Student(Convert.ToInt32(id), "", "", "", "", "", "", "");
if (e.CommandName == "Undergraduate")
student.setLevelRole(new UnderGraduateStudent());
else if (e.CommandName == "Graduate")
student.setLevelRole(new GraduateStudent());
student.writeLevelRole(id);
SqlConnection con = new SqlConnection(); //HERE IS DUPLICATION
//refresh the gridview on the page
con.ConnectionString = University.GetConnectionString();
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
con.Close();
}
The problem is, if i do not write the last 12 lines of the code, which is the same code as page_load method, the gridview does not refresh itself on the page. Waht can i do to avoid this?
Thanks
You should provide a method that loads the data and databinds the GridView, for example DataBindGrid:
private void DataBindGrid()
{
using(var con = new SqlConnection(University.GetConnectionString()))
{
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
}
}
Then you can call it from wherever you need. If you want to change something you have only one place to maintain which is less error-prone.
Note that you should databind the GridView only if(!IsPostBack) if you use ViewState(default):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback))
DataBindGridView();
}
and in RowCommand
protected void studentLevel_RowCommand(object sender, GridViewCommandEventArgs e)
{
// ... update student ... then
DataBindGridView();
}

Resources