I have a checkboxlist in asp.net, populated by a stored procedure. While "Select All" option is done in aspx.
Currently im setting my "Select All" as default using javascript to do some enabling and disabling upon slect "Select All".
My problem now is that I do not want to set "Select All" as default but the latest date as default on page load ? How can I do so when my dates are populated using stored procedure?
The below is how my checkboxlist lloks like with select all as default:
The below is what I want to achieve which is to get the latest date (2nd checkbox):
My codes for my checkboxlist:
<asp:CheckBoxList ID="Date_CBL" Class="checkboxlist" runat="server" AppendDataBoundItems="True" ViewStateMode="Enabled">
<asp:ListItem Text="Select All" Value="Select All"></asp:ListItem>
</asp:CheckBoxList>
My codes for javascript:
$(function () {
if ($("#hidden").val() == "") {
$("#Date_CBL :checkbox").removeAttr('disabled');
$("[id*=Date_CBL] input:checkbox").eq(1).prop("checked", true);
$("#hidden").val("set");
}
});
$(function () {
$("#Date_CBL :checkbox").change(function () {
var ischecked = $(this).is(":checked");
var val = $(this).val();
//alert(val);
if (val == "Select All") {
if (ischecked) {
$("#Date_CBL :checkbox").attr('disabled', 'disabled');
$(this).removeAttr('disabled');
$("#Date_CBL :checkbox").prop("checked", false);
$(this).prop("checked", true);
return;
} else {
$("#Date_CBL :checkbox").removeAttr('disabled');
return;
}
} else if (val != "Select All") {
if (ischecked) {
$("#Date_CBL :checkbox[value='Select All']").prop("checked", false);
}
}
});
});
My codes to populate my check box list:
using (SqlConnection conn = new SqlConnection(dbConn))
{
try //Call stored procedure
{
SqlCommand cmd = new SqlCommand(spddl, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//Populate CheckBoxList list items from store procedure
if (!IsPostBack)
{
Date_CBL.DataSource = ds.Tables[0];
Date_CBL.DataTextField = ds.Tables[0].Columns["DATA_DATE"].ToString();
Date_CBL.DataValueField = ds.Tables[0].Columns["DATA_DATE"].ToString();
Date_CBL.DataBind();
Bindgridview()
}
conn.Close();
}
My codes in cs:
public void BindGridView()
{
DateTime choosenDate = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(dbConn))
{
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
{
//Bind selected CheckBoxList items into one string and pass into stored procedure as parameter
String selectedDATE = String.Empty;
if (Date_CBL.SelectedValue == "Select All")
{
selectedDATE = "DATA_DATE";
}
else
{
foreach (ListItem item in Date_CBL.Items)
{
if (item.Selected)
{
DateTime dtTemp = Convert.ToDateTime(item.Value);
selectedDATE += "'" + dtTemp.ToString("yyyy-MM-dd") + "',";
}
}
selectedDATE = selectedDATE.Substring(0, selectedDATE.Length - 1);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@DATE", SqlDbType.VarChar).Value = selectedDATE;
string query = cmd.CommandText;
//Populate grid view
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Gridview1.DataSource = ds.Tables[0];
Gridview1.DataBind();
Thus how can I always set my latest date which will be the 2nd check box to be default upon page load?
Please advice thanks.
$(function () {
var $all = $("#Date_CBL ").find(":checkbox");
var $first = $all.eq(1);
$first.prop("checked", true);
});
You might need to go learn about Page Event
What load first...
UPDATE CODE
.cs
protected void Page_Load(object sender, EventArgs e)
{
// Variable
string dbConn = string.Empty;
string spddl = string.Empty;
// CHeck
if (!IsPostBack)
{
using (SqlConnection conn = new SqlConnection(dbConn))
{
// Error Handling
try { conn.Open(); }
catch (Exception ex) { throw ex; }
SqlCommand cmd = new SqlCommand(spddl, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
// Error Handling
try { da.Fill(ds); }
catch (Exception ex) { throw ex; }
Date_CBL.DataSource = ds.Tables[0];
Date_CBL.DataTextField = ds.Tables[0].Columns["DATA_DATE"].ToString();
Date_CBL.DataValueField = ds.Tables[0].Columns["DATA_DATE"].ToString();
Date_CBL.DataBind();
// Must Check From Code Behind since you are using it to before Page_Load.. Javascript will work after your Page Show Everything
Date_CBL.Items[1].Selected = true; // Check 2nd Item
BindGridView(conn);
conn.Close();
}
}
}
private void BindGridView(SqlConnection conn)
{
string spretrieve = string.Empty;
string selectedDATE = String.Empty;
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
{
//Bind selected CheckBoxList items into one string and pass into stored procedure as parameter
// This Will be wrong if you use selected Value
if (Date_CBL.Items[0].Selected == true)
{
selectedDATE = "DATA_DATE";
}
else
{
foreach (ListItem item in Date_CBL.Items)
{
if (item.Selected)
{
DateTime dtTemp = Convert.ToDateTime(item.Value);
selectedDATE += "'" + dtTemp.ToString("yyyy-MM-dd") + "',";
}
}
selectedDATE = selectedDATE.TrimEnd(','); // Don't use substring cause last length is the comma u can use trimEnd
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@DATE", SqlDbType.VarChar).Value = selectedDATE;
string query = cmd.CommandText;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try { da.Fill(ds); }
catch (Exception ex) { throw ex; }
Gridview1.DataSource = ds.Tables[0];
Gridview1.DataBind();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.