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
Related
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...
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal"
onitemcommand="DataList1_ItemCommand"
onselectedindexchanged="DataList1_SelectedIndexChanged"
ondatabinding="DataList1_DataBinding">
<ItemTemplate>
<asp:HiddenField ID="Hdnqid" runat="server" Value='<%# Bind("Id") %>' />
<asp:HiddenField ID="HidnResultStatus" runat="server" Value='<%# Bind("ResultStatus") %>' />
<asp:Button ID="Butto" runat="server" Text='<%#Eval("Id") %>' CommandName="Save&Next"
CommandArgument='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:DataList>
I want to change color of button accourding to value of table.
One way of doing this is using OnDataBound event of the data list. In the event you can get the button as well as your data item properties. Here you can change the properties of the button as below
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button button = e.Item.FindControl("Butto") as Button;
HiddenField HidnResultStatus = e.Item.FindControl("HidnResultStatus") as HiddenField;
string property = DataBinder.Eval(e.Item.DataItem, "colorproperty") as string;
//Here you can change the button color based on the value
if(HidnResultStatus.Value=="")
button.ForeColor = System.Drawing.Color.Black;
if(HidnResultStatus.Value=="1")
button.ForeColor = System.Drawing.Color.Brown;
}
I have a dataList with a command button, i want to click the button and obtain the value of a field in the datalist view of the button i have clicked.
I have tried various solutions but keep getting
{"Object reference not set to an instance of an object."}
here is my datalist
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1" Width="579px" OnItemCommand = "Datalist1_ItemCommand">
<ItemTemplate>
Epic:
<asp:Label ID="EpicLabel" runat="server" Text='<%# Eval("Epic") %>' />
<br />
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Company:
<asp:Label ID="CompanyLabel" runat="server" Text='<%# Eval("Company") %>' />
<br />
Date:
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
<br />
time:
<asp:Label ID="timeLabel" runat="server" Text='<%# Eval("time") %>' />
<br />
NewsItem:
<asp:Label ID="NewsItemLabel" runat="server" Text='<%# Eval("NewsItem") %>' />
<br />
HeadLine:
<asp:Label ID="HeadLineLabel" runat="server" Text='<%# Eval("HeadLine") %>' />
<br />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("Epic", "{0}") %>' Text="Button" />
<br />
</ItemTemplate>
</asp:DataList>
and her is my code behind
public void Datalist1_ItemCommand(object sender, DataListCommandEventArgs e)
{
var button = sender as Button;
/// if (button == null) return;
var dataListItem = button.NamingContainer as DataListItem;
if (dataListItem == null) return;
var currentKey = DataList1.DataKeys[dataListItem.ItemIndex];
var myLabel = button.Parent.Controls.Cast<Control>().FirstOrDefault(x => x.ID == "Epic") as Label;
/// if (myLabel == null) return;
var myLabelText = myLabel.Text;
}
You code do this using the following changes on your code;
on your button control:
<asp:Button ID="Button1" runat="server" CommandName="myCommand" Text="Button" />
on your ItemCommand event:
protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "myCommand":
// more code could go here
Label myLabel = (Label)e.Item.FindControl("EpicLabel");
var myLabelText = myLabel.Text;
// more code could go here
break;
}
}
If I am not mistaken, the ID of the control you are looking for is actually called EpicLabel, not just Epic. If myLabel is what the debugger is saying is not set to an instance of an object, this is most likely your problem.
So this line:
var myLabel = button.Parent.Controls.Cast<Control>().FirstOrDefault(x => x.ID == "Epic") as Label;
would need to be:
var myLabel = button.Parent.Controls.Cast<Control>().FirstOrDefault(x => x.ID == "EpicLabel") as Label;
New to asp.net so any help would be great Thank you.
I have a Datalist with has product name, price, Image and a linkbutton inside a datalist it also holds ProductID which is not displayed.
How do you get the current productID from onClick event of the LinkButton thank you.
My DataList
<asp:DataList ID="dlProduct" runat="server" DataKeyField="ProductID" RepeatDirection="Horizontal" RepeatColumns="4" CellPadding="10" CellSpacing="10" >
<ItemTemplate>
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />
<br />Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# "$" + Eval("Price") %>' />
<br />
<asp:Image ID="Image1" runat="server" CssClass="ImageStyles" ImageUrl='<%# "GetImage.ashx?Id=" + Eval("PID") %>' BackColor="White" BorderStyle="Ridge" BorderColor="WhiteSmoke" />
<br />
<asp:LinkButton ID="btnAddToCart" OnClick="btnAddToCart_Click" runat="server"><img src="images/AddToCart.png" onmouseover="this.src='images/AddToCartMouseOver.png';" onmouseout="this.src='images/AddToCart.png';" /></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
My Code behind
protected void btnAddToCart_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Product ID = "+ PID.Text + "');</script>", false);
}
Use the CommandArgument property of the LinkButton control.
Like:
<asp:LinkButton ID="btnAddToCart"
CommandName="AddToCart"
CommandArgument='<%# Eval("ProductId") %>'
OnCommand="btnAddToCart_Command" runat="server">
Then in the btnAddToCart_Command you can get command argument like this.
protected void btnAddToCart_Command(object sender, CommandEventArgs args)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Product ID = " + args.CommandArgument+ "');</script>", false);
}
Hope this helps.
I am creating an application, in which I am displaying the images(contained in a folder) in a datalist. Each datalist cell is having a ImageButton(clicking on which will show large pic of the image), a delete button(clicking which will delete the image), a edit button and a textbox. Clicking on the edit button will cause the imagefile name to get displayed in the textbox.
here is my .aspx code
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5" EnableViewState="True">
<ItemTemplate>
<asp:ImageButton Width="100" ID="ImagePic" ImageUrl='<%# Container.DataItem %>' CommandName='<%# Container.DataItem %>' runat="server" OnClick="ImagePic_Click" ImageAlign="Top">
</asp:ImageButton>
<br />
<asp:Button Width="100" ID="btn_image_del" CommandName='<%# Container.DataItem %>' runat="server" Text="Delete" OnClick="btn_image_del_Click">
</asp:Button>
<br />
<asp:TextBox ID="txt_image_name" Width="100" runat="server" Visible="True" Text='<%# Container.DataItem %>' MaxLength="500" />
</asp:TextBox>
<asp:Button Width="100" ID="btn_image_edit" CommandName='<%# Container.DataItem %>' runat="server" Text="Edit" OnClick="btn_image_edit_Click">
</asp:Button>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />
</asp:DataList>
And the .cs code is as follows
protected void BindDataList()//shows the pics from the user folder
{
string[] list = Directory.GetFiles(Server.MapPath("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/"));
var aList = from fileName in Directory.GetFiles(Server.MapPath("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/"))
select string.Format("/Candidate_Pics/" + Convert.ToString(Session["Sex"]) + "/" + txt_u_name.Text + "/{0}", Path.GetFileName(fileName));
dtlist.DataSource = aList;
dtlist.DataBind();
}
protected void ImagePic_Click(object sender, ImageClickEventArgs e)
{
string strImage = ((ImageButton)sender).CommandName;
ViewState["InsertedURL"] = strImage;
ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:ShowPic();", true);
Large_Pic.ImageUrl = strImage;
}
protected void btn_image_del_Click(object sender, EventArgs e)
{
string del_selected_image = ((Button)sender).CommandName;
File.Delete(Server.MapPath(del_selected_image));
}
protected void btn_image_edit_Click(object sender, EventArgs e)
{
string edit_selected_image = Path.GetFileName(((Button)sender).CommandName);
// Now what should i do:
}
The above three function namely ImagePic_Click,btn_image_del_Click, btn_image_edit_Click are working fine..Deletion, then LargePic view are all working perfectly, my problem is that, I want that when the edit button will be clicked, the corresponding name of the image will be displayed in the Datalist textbox. In the just above function, edit_selected_image is holding the filename of the corresponding image. I have tested it by applying breakpoints. Now the problem is that I want this value should be passed to the textbox "txt_image_name" in the Datalist.
Try this:
TextBox txt =(TextBox)dtlist.FindControl("txt_image_name");
txt.Text = edit_selected_image;
you need to use EditItemTemplate, for editing Edit Template.
you can write your mark up some tihing like this .
<EditItemTemplate>
PictureName:<asp:Label id="PicutreLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "yourFIleNamePath") %>' runat="server"/> <br />
<asp:LinkButton id="UpdateButton" Text="Update" CommandName="Update" runat="server"/>
<asp:LinkButton id="DeleteButton" Text="Delete" CommandName="Delete" runat="server"/>
<asp:LinkButton id="CancelButton" Text="Cancel" CommandName="Cancel" runat="server"/>