Cannot get value of UpdatePanel element - asp.net

I have a repeater inside an Update Panel. when I click the "Find Books" button repeater is modified according to the query.
What I want is to get the value of the DropDown when I click Add To Cart Button. But I can't get the value, instead the UpdatePanelAnimationExtender runs again. I added another button (TestingButton) inside the Update Panel. The UpdatePanelAnimationExtender runs when I click that button also.
My question is how can restrict the update panel only to the "Find Books" button? and how can I retrieve the value of DropDown outside the update panel? I added UpdateMode to conditional but it also not working.
aspx page
<p>Search by Author</p>
<asp:TextBox ID="txtAuthor" runat="server" Text="" AutoComplete="off" /><br />
<p>Search by Publisher</p>
<asp:TextBox ID="txtPublisher" runat="server" Text="" AutoComplete="off" /><br />
<asp:Button ID="btnFind" runat="server" Text="Find Books" OnClick="BtnFind_Click" />
<br />
<p>Search by Price</p>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Below Rs. 1000.00"></asp:ListItem>
<asp:ListItem Text="Between Rs. 1001 to 2500"></asp:ListItem>
<asp:ListItem Text="Above Rs. 2501.00"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button"
onclick="Button1_Click" />
<asp:UpdatePanel ID="udpBooks" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="btnTest" runat="server" Text="Testing Button" onclick="Button1_Click" />
<asp:Repeater ID="repBooks" runat="server" DataSourceID="SqlDataSource1"
onitemcommand="repBooks_ItemCommand">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div id="bookBorder">
<table width="640px" height="70px" border="0">
<tr>
<td width="51%" style="padding-top: -20px">
<span class="title">Description:</span> :
<%# DataBinder.Eval(Container.DataItem, "description")%>
</td>
<td>
<asp:DropDownList ID="DropDownList2" CssClass="dropDown" runat="server" Width="100px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnAddToCart" CssClass="button" runat="server" Text="Add to Cart"
OnClick="BtnAddToCart_Click" />
</td>
</tr>
</table>
</div>
<br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnFind" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanelAnimationExtender ID="upae1" runat="server" TargetControlID="udpBooks">
<Animations>
<OnUpdating>
<Parallel Duration="0">
<EnableAction AnimationTarget="btnFind" Enabled="false" />
<FadeOut MinimumOpacity=".5" />
</Parallel>
</OnUpdating>
<OnUpdated>
<Parallel Duration="0">
<FadeIn MinimumOpacity=".8" />
<EnableAction AnimationTarget="btnFind" Enabled="true" />
</Parallel>
</OnUpdated>
</Animations>
</asp:UpdatePanelAnimationExtender>
Code Behind
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqldsOrderDetails_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
string author = txtAuthor.Text;
string publisher = txtPublisher.Text;
string select = "";
if (author.Equals("") && publisher.Equals(""))
select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book]";
else if(author.Equals("") && !publisher.Equals(""))
select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where publisher=#publisher ";
else if(!author.Equals("") && publisher.Equals(""))
select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where author=#author";
else if(!author.Equals("") && !publisher.Equals(""))
select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where author=#author and publisher=#publisher";
SqlDataSource1.SelectCommand = select;
e.Command.Parameters.Add(new SqlParameter("#author", this.txtAuthor.Text));
e.Command.Parameters.Add(new SqlParameter("#publisher", this.txtPublisher.Text));
}
protected void BtnFind_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
// this.gvOrderDetails.DataBind();
this.repBooks.DataBind();
}
protected void ul_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}
protected void BtnAddToCart_Click(object sender, EventArgs e)
{
txtTest.Text = "sd";
}
protected void Button1_Click(object sender, EventArgs e)
{
txtTest.Text = "sd";
}
protected void repBooks_ItemCommand(object source, RepeaterCommandEventArgs e)
{
DropDownList d = e.Item.FindControl("DropDownList2") as DropDownList;
txtTest.Text = d.SelectedValue;
Button b= e.Item.FindControl("btnAddToCart") as Button;
b.Text = d.SelectedValue;
}
}

For triggering
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
Some content that needs to be updated here...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="FindBook" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
as far as the value of DropDown into textbox is concern, you should place textbox in another updatepanel and set its value when user select a value from dropdown by calling update method.

Related

Dropdown item select change event

I have dropdownlist1 containing items 2wheeler, 3wheeler.
If I select 2 wheeler dropdownlist2 should be open with items gear and non gear.
if I select gear I want to open textbox to write bike model name.
else if I select non gear I want to open just label.
Your design page:
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ShowDDL2" />
<asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="ShowControls" Visible="false" />
<asp:TextBox ID="TextBox1" runat="server" Visible="false" />
<asp:TextBox ID="TextBox2" runat="server" Visible="false" />
<asp:Label ID="Label1" runat="server" Visible="false" />
<asp:Label ID="Label2" runat="server" Visible="false" />
Your code behind:
protected void ShowDDL2(object sender, EventArgs e){
ddl2.Visible = true;
}
protected void ShowControls(object sender, EventArgs e){
TextBox1.Visible = TextBox2.Visible = Label1.Visible = Label2.Visible = true;
}
Is this what you need? But I don't see a logic in this...

Refresh Captcha Code with Updatepanel control in ASP>NET

I have a captcha code in my site.
sometimes needs to chenge that picture , but i dont want the whole page refreh.
i used updatepanel in this way :
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Check" />
<asp:Button ID="Button3" runat="server" Text="refresh" OnClick="Button3_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Here is Button3 code behind :
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "~/CImage.aspx";
}
Button1 Works correctly But Button3 which should change the captcha picture doesn't work.
Am I missing something?
In addition if Iwant to use a refresh image instead of Button3,What controls should I use?
The image likely isn't updating during partial postback because the browser doesn't realize that the image content has changed since it been targeted to same page over and over. Adding querystring might help in this case one need to pass random id as querystring to the ImageUrl of the captcha.
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = string.Format("~/CImage.aspx?img={0}", Guid.NewGuid());
}
There is nothing wrong with your code Kindly check if "~/CImage.aspx" is returning correct values or not. I have modified your code Kindly check:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image Width="100" Height="100" ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" Text="Check" />
<asp:Button ID="Button3" OnClick="Button3_Click" runat="server" Text="refresh" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void Button3_Click(object sender, EventArgs e)
{
if (Image1.ImageUrl == "Koala.jpg")
{
Image1.ImageUrl = "Close.png";
}
else
{
Image1.ImageUrl = "Koala.jpg";
}
}

Visible property not showing the controls

I have created an aspx page in which I want some controls to be enabled on basis of user selection.
If user selects All two radio buttons should be enabled, hide otherwise.
My declarative part is:
<tr>
<td>
<asp:Label ID="lblCommunityMembers" runat="server" Text="Community Members" />
</td>
<td>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="upCommunityMembers" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdbCommunityMembersAll" runat="server" Text="All" GroupName="grpCommMembers" Checked="true" OnCheckedChanged="rdbCommunityMembersAll_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="rdbCommunityMembersSelectedUsers" runat="server" Text="Selected Users" GroupName="grpCommMembers" OnCheckedChanged="rdbCommunityMembersSelectedUsers_CheckedChanged" AutoPostBack="true" />
<SharePoint:ClientPeoplePicker ID="ppCommunityMembers" runat="server" AllowMultipleEntities="true" AllowEmpty="false" Visible="false" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCommunityCatagory" runat="server" Text="Community Catagory" />
</td>
<td>
<asp:DropDownList ID="ddlCommunityCatagory" runat="server">
<asp:ListItem Value="0">---- Select One ----</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvCommunityCatagory" runat="server" InitialValue="0" ErrorMessage="Please Select Community Catagory"
ForeColor="Red" ControlToValidate="ddlCommunityCatagory" Display="Dynamic" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCommunityAccess" runat="server" Text="Required Approval?" Visible="false" />
</td>
<td>
<asp:RadioButton ID="rdbRequiredApprovalYes" runat="server" Text="Yes" GroupName="grpCommMembers" Checked="true" Visible="false" />
<asp:RadioButton ID="rdbRequiredApprovalNo" runat="server" Text="No" GroupName="grpCommMembers" Visible="false"/>
</td>
</tr>
My code behind:
protected void rdbCommunityMembersSelectedUsers_CheckedChanged(object sender, EventArgs e)
{
if (rdbCommunityMembersSelectedUsers.Checked)
{
enableControls();
}
else
{
disableControls();
}
}
protected void rdbCommunityMembersAll_CheckedChanged(object sender, EventArgs e)
{
if (rdbCommunityMembersAll.Checked)
{
disableControls();
}
else
{
enableControls();
}
}
protected void enableControls()
{
ppCommunityMembers.Visible = true;
lblCommunityAccess.Visible = true;
rdbRequiredApprovalNo.Visible = true;
rdbRequiredApprovalYes.Visible = true;
}
protected void disableControls()
{
ppCommunityMembers.Visible = false;
lblCommunityAccess.Visible = false;
rdbRequiredApprovalNo.Visible = false;
rdbRequiredApprovalYes.Visible = false;
}
If Community members are selected to all then "Required Approval?" part should get hidden.
But problem is when I select selected users then I am getting only people picker control visible, the required approval contorls are not getting displayed. What am I missing?
Your Radio Buttons rdbCommunityMembersAll and rdbCommunityMembersSelectedUsers are inside update panel, thus they are doing partial postback. To make the control visible outside the UpdatePanel you can do any one of the following:
Move rdbCommunityMembersAll and rdbCommunityMembersSelectedUsers
outside the UpdatePanel
Move the controls you need to make visible
(lblCommunityAccess, rdbRequiredApprovalNo, rdbRequiredApprovalYes)
inside UpdatePanel
Or
Instead of setting Visible="false", set style="display:none" and
trigger the visibility through javascript/jquery

Can not get textbox value inside datalist

I am trying to get the value of textbox "Qty" inside a datalist. It does not work. What is wrong? I have both the CartItem label and the datelist inside the ajax updatepanel. Thanks for any help. Here is my code:
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10"
DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart"
onClick="ButtonAddToCart_Click"/>
<br />
</ItemTemplate>
</asp:DataList>
and here is the button click event. The CartItem has null value:
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
CartItem.Text = DataList1.FindControl("Qty").ToString();
}
TextBox Qty = (TextBox)DataList1.FindControl("Qty");
if(Qty != null)
{
CartItem.Text =Qty.Text;
}
You can use NamingContainer or Parent to access sibling controls
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
var button = sender as Button;
var textbox = button.NamingContainer.FindControl("Qty") as TextBox;
CartItem.Text = textbox.Text;
}
I prefer you to use item_command event of the datalist
do some think like this.
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" OnCommand="DataList1_ItemCommand"
/>
<br />
</ItemTemplate>
</asp:DataList>
Here is the item_command event that works.
public void DataList1_ItemCommand(object source, System.Web.UI.WebControls.CommandEventArgs e){
TextBox qtytxtbox = DataList1.FindControl("Qty") as TextBox;
}
You can do like this
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
Button ButtonAddToCart= (Button)sender;
DataListItem item = (DataListItem)ButtonAddToCart.NamingContainer;
var textbox = (TextBox)item.FindControl("Qty");
}
try this one
TextBox txtquantity = (TextBox)(e.Item.FindControl("Qty"));
simply use this......You will get the value in txtquantity

Working with dropdown list & Validation in asp.net

I have got a dropdown list populated with products from a product table. I have a button named ADD Product, so when the user wants to add a new product instead of selecting product from the drop down list, one should click on add new then panel 1 should show which includes a textbox. Panel1 visibility is set to false by default and it should be visible when the user clicks on the button. Also I want a validation on dropdown list and textbox(if panel is visible). Below is the code which is not working :
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<%--<asp:TextBox ID="txtMake" runat="server" CssClass="txtStyle"></asp:TextBox>--%>
<asp:Button ID="btnAdd" runat="server" />
<asp:Panel ID="panel1" Visible="false" runat="server"><asp:TextBox ID="txtAddnew"></asp:TextBox></asp:Panel>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Select a Product" ValidationGroup="insert" ControlToValidate="DropDownList1" ForeColor="Red"></asp:RequiredFieldValidator><br />
codebehind:
protected void btnAdd_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
DropDownList1.DataSource = caravans.GetProductNames();
DropDownList1.DataBind();
}
}
You can use this concept for validation on dropdownlist. I am given you an example please check this...
<asp:DropDownList runat="server" ID="ddl">
<asp:ListItem Value="-1" Text="Select"></asp:ListItem>
<asp:ListItem Value="1" Text="One"></asp:ListItem>
<asp:ListItem Value="2" Text="Two"></asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="reg1" runat="server" ControlToValidate="ddl"
Display="Dynamic" ErrorMessage="Select Proper" SetFocusOnError="true" Text="Select Proper"
ValidationGroup="check" ValidationExpression="^\d{0,5}$" />
<asp:Button ID="btn" runat="server" ValidationGroup="check" Text="Submit" />

Resources