Update Panel Button need to be pressed twice to update - c#

I have a List View inside an update panel, but I can't get it to update properly - each item has a button that removes it from the list - which it does but it takes two button presses to see the item disappear from the page.
Here the markup:
<asp:ScriptManager ID="DashScriptManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="ToDoUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListView ID="ToDo" runat="server">
<ItemTemplate>
<li style="" class="<%# Eval("ToDoPriority")%>">
<%# Eval("ToDoText")%>
<div class="agile-detail">
<asp:LinkButton ID="ToDoComplete" runat="server" CssClass="pull-right btn btn-xs btn-primary" Text="Done" OnClick="ToDoComplete_Click"></asp:LinkButton>
<i class="fa fa-clock-o"></i> <%# Eval("ToDoDate")%>
</div>
</li>
<asp:HiddenField ID="HiddenToDoID" runat="server" Value='<%# Eval("ToDoId") %>' />
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
And the Code Behind:
protected void ToDoComplete_Click(Object sender, EventArgs e)
{
LinkButton ToDoComplete = sender as LinkButton;
HiddenField todo = ToDoComplete.NamingContainer.FindControl("HiddenToDoID") as HiddenField;
int TodDoId = Convert.ToInt32(todo.Value);
DashboardController.UpdateToDo(TodDoId);
GetToDoItems(1);
ToDoUpdate.Update();
}
Is there any way to do this by pressing the button once?

What you are missing is Triggers section in UpdatePanel. As your update panel as UpdateMode ="Conditional" you need to specify triggers. If you are using normal full postback then change AsyncPostBackTrigger to PostBackTrigger.
<asp:UpdatePanel ID="ToDoUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListView ID="ToDo" runat="server">
<li style="" class="<%# Eval("ToDoPriority")%>">
<%# Eval("ToDoText")%>
<div class="agile-detail">
<asp:LinkButton ID="ToDoComplete" runat="server" CssClass="pull-right btn btn-xs btn-primary" Text="Done" OnClick="ToDoComplete_Click"></asp:LinkButton>
<i class="fa fa-clock-o"></i> <%# Eval("ToDoDate")%>
</div>
</li>
<asp:HiddenField ID="HiddenToDoID" runat="server" Value='<%# Eval("ToDoId") %>' />
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ToDo" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

Related

Get TextBox Text value in a button

I'm trying to get the value of txt_MRP in the codebehind but it keeps giving me
The name "txt_MRP" doesn't exist in this current context
Why is this reference not available? How can I get it?
(The button that I am pressing is btn_add_to_cart_Click)
<div class="row">
<div class="col-lg-2"></div>
<div class="col-lg-4">
<asp:DataList ID="DataList3" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<ul id="etalage">
<li>
<img id="Img1" class="etalage_thumb_image" runat="server" src='<%# Eval("Fotografia1") %>' />
<img id="Img2" class="etalage_source_image" runat="server" src='<%# Eval("Fotografia2") %>' />
</li>
</ul>
<br />
</ItemTemplate>
</asp:DataList>
</div>
<div class="col-lg-6">
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<div id="product_name_width">
<asp:Label ID="Product_nameLabel" runat="server" CssClass="product_name" Text='<%# Eval("Nombre") %>' />
<br />
</div>
<span id="rs">₡</span><asp:Label ID="MRPLabel" CssClass="price" runat="server" Text='<%# Eval("Precio") %>' />
<br />
<br />
<span id="brand_name">Marca</span>
<asp:Label ID="BrandLabel" runat="server" CssClass="brand" Text='<%# Eval("Marca") %>' />
<br />
</div>
<div class="form-group">
<label> Cantidad </label>
<asp:TextBox CssClass="form-control" ID="txt_MRP" runat="server" ></asp:TextBox>
</div>
<div id="buy_now_css" runat="server">
<ul>
<li>
<asp:Button ID="btn_add_to_cart" CssClass="btn btn-primary" runat="server" Text="Añadir al Carrito" OnClick="btn_add_to_cart_Click" />
</li>
<li>
<asp:Button ID="btn_buy_now" CssClass="btn btn-success" runat="server" Text="Comprar" OnClick="btn_buy_now_Click" />
</li>
</ul>
</div>
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="DataList2" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h2>Descripcion</h2>
<div id="Description_width">
<asp:Label ID="DetailLabel" CssClass="description" runat="server" Text='<%# Eval("Descripcion") %>' />
</div>
</ItemTemplate>
</asp:DataList>
</div>
</div>
txt_MRP does not exist because it is in a DataList Control. It is only available inside the scope of the Control.
If you want to get the value you have to use another method.
Change the button event from Onclick to OnCommand and add a CommandName and set the ItemIndex as CommandArgument.
<asp:Button ID="btn_add_to_cart" CommandName="addToCart" CommandArgument='<%# Container.ItemIndex %>' CssClass="btn btn-primary" runat="server" Text="Añadir al Carrito" OnCommand="btn_add_to_cart_Command" />
And in code behind
protected void btn_add_to_cart_Command(object sender, CommandEventArgs e)
{
//check the correct commandname
if (e.CommandName == "addToCart")
{
//get the row number from the commandargument
int itemIndex = Convert.ToInt32(e.CommandArgument);
//find the texbox in the correct row with findcontrol
TextBox textBox = DataList1.Items[itemIndex].FindControl("txt_MRP") as TextBox;
//do stuff with the value
Label1.Text = textBox.Text;
}
}
You have to assign the delegate or button. For instance:
var button = new Button();
button.Click += new EventHandler("btnAddToCart");
You also may need a CommandArgument, to be able to send a specific value back. But to actually register the click event would be like the above.

Response of link button inside update panel is too late in asp.net C#

When I click on Flag button all operation to be perform by button click are working very well but it take nearly 7 8 seconds to Show effects on screen..
My Asp.net code:
<asp:ScriptManager runat="server" EnablePartialRendering="true" ID="SC1"></asp:ScriptManager>
<!-- Get Mile-->
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-7">
<%-- <asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>--%>
<ul class="timeline-list">
<asp:Repeater runat="server" ID="repeater_post" OnItemDataBound="repeater_post_ItemDataBound" OnItemCreated="repeater_post_ItemCreated">
<ItemTemplate>
<li class="media media-clearfix-xs">
<div class="media-body">
<div class="media-body-wrapper">
<div class="panel panel-default ">
<asp:Image runat="server" ID="img_PostPic" ImageUrl='<%# GetImagePath(Eval("ImagePath").ToString()) %>' CssClass="img-responsive" Style="height: 200px !important; width: 526px !important" />
<%--<asp:Image runat="server" ID="Image1" ImageUrl='<%# GetMood(Eval("Mood").ToString()) %>' class="pull-right" />--%>
<div class="view-all-comments">
<asp:UpdatePanel runat="server" ID="upd5" UpdateMode="Always">
<ContentTemplate>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("PostID").ToString() %>' ID="lbtn_Flag" CssClass="btn btn-white" ToolTip='<%# GetAllFlagMember(Convert.ToString(Eval("PostID"))) %>' OnClick="lbtn_Flag_Click" Visible='<%# CheckFlag(Eval("PostID"))%>'>
<i class="fa fa-flag"></i>
</asp:LinkButton>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("PostID") %>' ID="lbtn_Unflag" CssClass="btn btn-white" ToolTip='<%# GetAllFlagMember(Convert.ToString(Eval("PostID"))) %>' OnClick="lbtn_Unflag_Click" Visible='<%# CheckUnFlag(Eval("PostID"))%>'>
<i class="fa fa-flag"></i> Unflag
</asp:LinkButton>
<strong> <%# GetFlagCount(Eval("PostID").ToString())%> Flag</strong>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbtn_Flag" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lbtn_Unflag" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<%--<asp:LinkButton runat="server" ID="lbtnflagmember" CommandArgument='<%# Eval("PostID")%>' Text='<%# GetFlagCount(Eval("PostID").ToString())%>'></asp:LinkButton>--%>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_sharing_toolbox" style="float: right"></div>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("Lat")+"$"+Eval("Long")+"$"+Eval("PostAutorID")+"$"+Eval("Destination") %>' ID="lbtn_MapCanvas" ToolTip="Map" OnClick="lbtn_MapCanvas_Click" CssClass="pull-right" CausesValidation="false"> <i class="fa fa-map-marker"></i> Navigate
</asp:LinkButton>
<asp:LinkButton runat="server" CommandArgument='<%# Eval("PostID")+"$"+Eval("Destination") %>' ID="lbtn_ReportAbuse" ToolTip="Report Abuse" OnClick="lbtn_ReportAbuse_Click" CssClass="pull-right"> <i class="fa fa-share-square-o "></i> Report Abuse
</asp:LinkButton>
</div>
</div>
</div>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
My C# Back end Code on link buttons is :
protected void lbtn_Flag_Click(object sender, EventArgs e)
{
//lbtn_Flag.Enabled = false;
//lbtn_Unflag.Enabled = true;
LinkButton btn = (LinkButton)sender;
int _PostId = Convert.ToInt32(btn.CommandArgument);
WStblFlag wsFlag = new WStblFlag();
string res = wsFlag.AddFlag(_PostId, Functions.ParseInteger(Convert.ToString(Session["ProfileID"])));
GetPost();
//UpdatePanel up = repeater_post.FindControl("upd5") as UpdatePanel;
//up.Update();
//UpdatePanel inner_rpt = (UpdatePanel)repeater_post.Items.FindControl("repeater_Comment");
}
/// <summary>
/// UnFlag the post
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtn_Unflag_Click(object sender, EventArgs e)
{
//lbtn_Flag.Enabled = true;
//lbtn_Unflag.Enabled = false;
LinkButton btn = (LinkButton)sender;
int _PostId = Convert.ToInt32(btn.CommandArgument);
WStblFlag wsFlag = new WStblFlag();
string res = wsFlag.UnFlagPost(_PostId, Functions.ParseInteger(Convert.ToString(Session["ProfileID"])));
GetPost();
}
I already try many things to reduce response time but it wont succeed...
Ty..

Access TextBox in DataList by ID from Button-Click handler in codebehind

I have a textbox which is kept inside Datalist. I need to find it via ID, so that i can insert text written to that textbox to the database.Here is my aspx page containing textbox.
<asp:Content ID="Content1" ContentPlaceHolderID="ccont" Runat="Server">
<div id="ccont">
<asp:DataList ID="mydatalist" ItemStyle-CssClass="lft_c_down" runat="server">
<ItemTemplate>
<div id="wholeC">
<div id="ctop">
<div id="lft_l">
<div id="lft_c_top">
<asp:Image runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ipath")%>' Height="250px" Width="300px" />
<br/>
</div>
<div id="lft_c_down">
<b>Product Name:</b>
<asp:Label ID="lbl2" Text='<%#DataBinder.Eval(Container.DataItem,"products") %>' runat="server" />
<br/>
<b>brand:</b>
<asp:Label ID="lbl1" Text='<%#DataBinder.Eval(Container.DataItem,"brand") %>' runat="server" />
<br/>
<b>Price:</b>
<asp:Label ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem,"price") %>' runat="server" />
</div>
</div>
<div id="lft_r">
<b>Details:</b>
<asp:Label ID="Label2" Text='<%#DataBinder.Eval(Container.DataItem,"description") %>' runat="server" />
</div>
</div>
<div id="cmt">
<asp:TextBox ID="tb_cmt" runat="server" Height="35px" Width="620" placeholder="comment.." />
<asp:Button ID="Button1" runat="server" Text="Comment" backcolor="black" BorderStyle="None" Font-Names="Consolas" Font-Overline="False"
ForeColor="White" Height="34px" Width="108px" OnClick="cmt_Click" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
</div>
The Textbox with ID="tb_cmt" is the text box i want to access in my code behind as:
protected void cmt_Click(object sender, EventArgs e)
{
// how to get the TextBox?
sq.connection();
SqlCommand cmd = new SqlCommand("insert into comment(ecomment,sid) values(#myecomment,#mysid)", sq.con);
cmd.Parameters.AddWithValue("#myecomment",tb_cmt.text)//but here tb_cmt is not recognized.
}
You can use the NamingContainer property of the button that was clicked to get the DataListItem. Then you just have to use FindControl to get the reference to your TextBox:
protected void cmt_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
DataListItem item = (DataListItem) btn.NamingContainer;
TextBox txt = (TextBox) item.FindControl("tb_cmt");
//... save
}

Div show hide not working when using UpdatePanel inside dev

<div id="test1" runat="server">
<asp:UpdatePanel runat="server" ID="updTerms">
<ContentTemplate>
<asp:Button ID="btnSubmit" Enabled="false" CssClass="asgButton" runat="server" Text="Submit" onclick="btnSubmit_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div id="test2" visible="false" runat="server">
<p>this is sample text.... bla bla bla</p>
</div>
C# Code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid) {
// ...
test1.Visible = false;
test2.Visible = true;
} else {
// do some thing...
}
}
Only elements inside of the update panel will be updated when an update panel is triggered.
Since you only have the button in the update panel, the button is the only thing that will get updated, even though you set it in the code behind. You just have to wrap all the elements you want updated in the panel like so.
<asp:UpdatePanel runat="server" ID="updTerms">
<ContentTemplate>
<div id="test1" runat="server">
<asp:Button ID="btnSubmit" Enabled="false" CssClass="asgButton" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</div>
<div id="test2" visible="false" runat="server">
<p>this is sample text.... bla bla bla</p>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Try following Code:
<asp:UpdatePanel runat="server" ID="updTerms">
<ContentTemplate>
<div id="test1" runat="server">
<asp:Button ID="btnSubmit" Enabled="True" CssClass="asgButton" runat="server" Text="Submit" OnClick="btnSubmit_OnClick"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="updDiv2" UpdateMode="Conditional">
<ContentTemplate>
<div id="test2" visible="false" runat="server">
<p>this is sample text.... bla bla bla</p>
</div>
</ContentTemplate>
</asp:UpdatePanel>
And in code-behind:
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
test1.Visible = false;
test2.Visible = true;
updDiv2.Update();
}
}
If you update controls from an UpdatePanel they have to be either in the same UpdatePanel or in another one.
If the controls are in another one make sure to call Update of the UpdatePanel containing the controls after editing them.
Make sure as well to set UpdateMode="Conditional" on the UpdatePanel otherwise Update would throw an exception.
Cover your complete div inside update panel..You should place the controls which have to be updated inside update panel..
<asp:UpdatePanel runat="server" ID="updTerms">
<ContentTemplate>
<div id="test1" runat="server">
<asp:Button ID="btnSubmit" Enabled="false" CssClass="asgButton" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</div>
<div id="test2" visible="false" runat="server">
<p>this is sample text.... bla bla bla</p>
</div>
</ContentTemplate>
</asp:UpdatePanel>
My suggestion is do not use Visible property.Instead do the following things
1)Remove Visibile="False" .Use Style="display:none"
<div id="test2" style="display:none" runat="server">
2)Add the following javascript in the design page.
function divviZ()
{
var test1=document.getElementById("<%=test1.ClientId%>");
var test2=document.getElementById("<%=test2.ClientId%>");
if(test2.style.display=="none")
{test1.style.display="block";}
else
{test1.style.display="none";}
}
3)Change the vb code as follows
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid) {
// ...
test2.style("display")="block"
ScriptManager.RegisterClientScriptBlock(this, this.GetType, "divviz", "javascript:divviZ()", True)
} else {
// do some thing...
}
}

How to bind radiobuttonlist with jquery

I have added radiobuttonlist into asp updatepanel, radiobuttonlist is bound form table in codebehind with C#.But updatepanel gives error at postback.can anybody help me to bind radiobuttonlist with Jquery on page load.
<asp:UpdatePanel ID="UpdPanel_Questions" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<asp:Label ID="Lbl_QuestionTitle" runat="server" Font-Bold="False"
Font-Size="Medium" Height="45px" ></asp:Label>
</div>
<div> <p></p>
<asp:RadioButtonList ID="RadBut_Answer" runat="server" onselectedindexchanged="RadBut_Answer_SelectedIndexChanged"
CellSpacing="5" AutoPostBack="True" CellPadding="0">
</asp:RadioButtonList>
</div>
<div class="SelectedAnsMsg alpha">
<asp:Literal runat="server" EnableViewState="False" ID="Lbl_SelectedAnsMsg"></asp:Literal>
</div>
<div class="Butskipnext">
<div class="Butskip">
<asp:Button ID="But_Skip" runat="server" Text="Skip" SkinID="AltButton" />
</div>
<div class="Butnext">
<asp:Button ID="But_Next" runat="server" Text="Next" SkinID="Button"
onclick="But_Next_Click" />
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RadBut_Answer" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Why do you want to use jQuery to bind RadioButtonList at Page_Load? If you want to bind RadioButtonList on Page_Load using jQuery, then it is something to do in Javascript (i.e. either in .aspx page or separate Javascript file), but not code behind.
I have not used jQuery, but the following code in code-behind works well to bind table data to RadioButtonList.
For the example purpose, I have Taken DEPT(DEPTNO, DNAME) table.
Now, I hope you can understand the code since it is very easy.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlCommand objCmd = new SqlCommand("SELECT DEPTNO, DNAME FROM DEPT", objConn);
objConn.Open();
RadBut_Answer.DataSource = objCmd.ExecuteReader();
RadBut_Answer.DataTextField = "DNAME";
RadBut_Answer.DataValueField = "DEPTNO";
RadBut_Answer.DataBind();
objConn.Close();
}
}

Categories

Resources