Manoj Maharana Manoj Maharana - 3 months ago 34
C# Question

hide a div and lable after 5 second using javascript/jquery function in c#

Here is the div:

<div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<asp:Label runat="server" ID="lblmsg" Font-Bold="true" Visible="false"></asp:Label>
</div>


I want to hide the div after few second using client side code.
Here i am using c# in backend.

else if (e.CommandName.Equals("CategoryDelete"))
{
objCategory.CategoryDelete(Convert.ToInt32(e.CommandArgument));
lblmsgid.Visible = true;
lblmsg.Text = "Delete Successful..";
BindCategory();
}


After click on delete button from my grid i want to show a error message in the above lable(lable is in the div) and after few second i want to hide the div..
how can i do??

here is the button for activate:

<asp:Button ID="btnActivate" runat="server" OnClick="BtnActivate_Click" CssClass="btn-active" Text="Activate Selected" OnClientClick="javascript:return TestCheckBox();" />


here is the c# code:

protected void BtnActivate_Click(object sender, EventArgs e)
{
clsCategoryBL objproject = new clsCategoryBL();
foreach (GridViewRow gvr in this.gvCategory.Rows)
{
if (((CheckBox)gvr.FindControl("chkSelectRow")).Checked == true)
{
ImageButton btnStatus = (ImageButton)gvCategory.Rows[gvr.RowIndex].Cells[0].FindControl("imgStatus");
if (btnStatus.CommandArgument == "False")
{
HiddenField hdnProjectID = (HiddenField)gvCategory.Rows[gvr.RowIndex].Cells[0].FindControl("hdnProjectID");
// int Id = Convert.ToInt32(gvCategory.DataKeys[gvr.RowIndex].Values[0]);
int Id = Convert.ToInt32(hdnProjectID.Value);
objproject.CategoryStatus(Id, true);
lblmsgid.Visible = true;
}

}
lblmsgid.Visible = true;
lblmsg.Text = "Activated Successful..";
lblmsg.Visible = true;


}
lblmsgid.Visible = true;

BindCategory();

}


here is :

function TestCheckBox() {
if (TargetBaseControl == null) return false;

//get target child control.
var TargetChildControl = "chkSelectRow";

//get all the control of the type INPUT in the base control.
var Inputs = TargetBaseControl.getElementsByTagName("input");

for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
Inputs[n].checked)
return true;

alert('Select at least one checkbox!');
return false;
}

Answer

Simply use setTimeout. With plain js:

setTimeout(function() {
    var alerts = document.getElementsByClassName("alert-dismissable");
  
    for( var i = 0; i < alerts.length; i++ ) {
        alerts[i].style.display = "none";
    }
}, 2000);
<div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
  <label runat="server" ID="lblmsg" Font-Bold="true" Visible="false">asd</label>
</div>

Or with jQuery:

setTimeout(function() {
    $(".alert-dismissable").hide();
}, 2000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
  <label runat="server" ID="lblmsg" Font-Bold="true" Visible="false">asd</label>
</div>