Asp.Net ListView passing parameters from a TextBox - c#

Hi folks i have this terrible problems i hope any of you could help me first of all i have a ListView which has (lets say) 3 columns Id, Quantity, And an image button with an action.
the image button launch the function createparameter that splits the string into 2 int variables, but i want to get the first 1 from the textbox QuantityTextBox, What can i do to send the value of the textbox to the function, any ideas?
protected string CreateParameter(object arg1, object arg2)
{
return string.Concat(arg1, "|", arg2);
}
i wish there's a better way to do this than do a foreach of every list item and then find control because i dont care the other list items,

What you want to do will not work the way you want because the value of the textbox could be changed by the user. So it's not reasonable to try and bind it to the CommandArgument at runtime because there is no value there yet.
But when you handle the ItemCommand event for the ListView, you have access to which ListViewItem the button was clicked in, so you can use FindControl to get the textbox and look at its value.
For that to work, you will need to add a CommandName value to your ImageButton controls. And you would just pass in the product id in the command argument directly.
So, your button would look like this:
<asp:ImageButton ID="btnAdd" runat="server" Tooltip="Aggregar producto" CommandName="AddProduct"
CommandArgument="<%# Eval("IdProduct") %>" />
And your codebehind would look something like this (not tested):
protected void lv_Productos_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddProduct"))
{
// Get a reference to your textbox in this item
TextBox textbox = e.Item.FindControl("QuantityTextBox") as TextBox;
if (textbox != null)
{
int quant = 0;
if (int.TryParse(textbox.Value, quant))
{
int prodId = (int)e.CommandArgument;
//do what you want with the quantity and product id
}
}
}
}

Related

Hide DataList row based on value from web service

I am retrieving a set of value from a web service and populating a dataList with those values-
ActiveDataList.DataSource = ws.TermsReturnActive(sql);
ActiveDataList.DataBind();
How can I hide a particular column of the dataList depending on the value,
e.g.
if(value == 1)
{
//Hide Column
}
This action however would have to hide the same row of another dataList in parallel to it.
I can modify a cell on this second dataList using by retrieving the value from the first as so -
TextBox tb1 = (TextBox)sender;
DataListItem item1 = (DataListItem)tb1.NamingContainer;
TextBox txt1 = (TextBox)tData.Items[item1.ItemIndex].FindControl("tTextBox");
string term = txt1.Text;
So if I can retrieve a value from a separate dataList row, I was thinking I would also be able to adjust its visability.
How can I achieve this as the web service call is done in the page load so I believe it would have to be done when the dataList item is bound?
if iam right, you should have something like this in your aspx-file right?:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"></asp:DataList>
As you can see you should add a "OnItemDataBound" Event where you can check your value and hide a item, if you want.
So you can react like this and hide some items:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
TextBox tbCurrentTextBox = (TextBox)e.Item.FindControl("tTextBox");
if (DataList1.Items[e.Item.ItemIndex].ToString() == "1")
{
e.Item.Visible = false;
}
}
}

get value on edititemtemplate from codebehind

I have a detailsview where I get couple of data from membership profile and I display it on detailsview...this works fine:
<ItemTemplate>
<asp:label ID="FirstName" runat="server" />
</ItemTemplate>
But when I click the edit button, nothing shows up on the field. This is what I am doing on Edit template:
I call ItemUpdating like this:
protected void DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
//I get my memberprofle here
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
Label labelfName = DetailsView1.FindControl("FirstName") as Label;
labelfName.Text = memberp.fName;
}
Should I be using Itemupdated instead? Or is there another method that I should call when the edit button is clicked that will populate the firstname field on edit? Also, the reason I am keeping it as "LABEL"(usually it would be textbox) on edit mode is that this field has to be read only.
The event ItemUpdating is not fired when you enter on edit mode. You must use DataBound event to set properly required text value.
If neccesary, you can ask CurrentMode property of DetailsView to know if you are editing or displaying.
The result looks like this:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
Label l = DetailsView1.FindControl("FirstName") as Label;
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//obtained from your sample
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
l.Text = memberp.fName;
}
else
{
l.Text = "Another text or nothing";
}
}
Be sure to define DataBound event in you Detailsview1 control.
REMARK: It can be affected depending the data bind mode. If so, let me know and put an example.
Add RowUpdating and RowEditing event to your gridview.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

Obtaining the ctl property from a gridview control in C#, ASP.Net

I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.
You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}

How to find a label in a asp repeater

Structure of my asp: repeater
repeater
updatePanel
label1 (rating)
button (updates rating)
some_picture (thing being rated)
/update panel
/repeater
Imagine the output of the above repeater containing 100 rows. (1 label, and 1 button on each row).
Goal: when I click the button, I want the appropriate label to be updated. I dont know how to do this.
I can reference a label via:
Label myLabel2Update = (Label)Repeater1.Controls[0].Controls[0].FindControl("Label1");
But ofcourse, it will be the same label each time (not necessarily the label that needs to be updated). I need to update the label that is on the same row as the button.
Any guidance would be appreciated.
Handle the ItemCommand event of the repeater. In your event handler check the Item property of the event arguments and use findcontrol on that. e.g.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label Label1 = (Label)e.Item.FindControl("Label1");
}
Label1 will be the label in the same item as the button that was clicked.
Or in response to Dr. Wily's Apprentice's comment you could do the following
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "bClick":
Label Label1 = (Label)e.Item.FindControl("Label1");
/*do whatever processing here*/
break;
}
}
And then for each button specify the command name "bClick"
You will need a helper method to iterate through the hierarchy or use the
Control's FindControl(string id) method for that.
Example:
var stateLabel = (Label)e.Row.FindControl("_courseStateLabel");
I assume there's an event handler for the button? If so, you should be able to do
protected virtual void OnClick(object sender, EventArgs e)
{
var label = ((WebControl)clickedButton).Parent.FindControl("Label1");
}

page variable in a repeater

Hi I'm having a bit of an issue with a asp.net repeater
I'm building a categories carousel with the dynamic categories being output by a repeater.
Each item is a LinkButton control that passes an argument of the category id to the onItemClick handler.
a page variable is set by this handler to track what the selected category id is....
public String SelectedID
{
get
{
object o = this.ViewState["_SelectedID"];
if (o == null)
return "-1";
else
return (String)o;
}
set
{
this.ViewState["_SelectedID"] = value;
}
}
problem is that i cant seem to read this value while iterating through the repeater as follows...
<asp:Repeater ID="categoriesCarouselRepeater" runat="server"
onitemcommand="categoriesCarouselRepeater_ItemCommand">
<ItemTemplate>
<%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "<div class=\"selectedcategory\">":"<div>"%>
<asp:LinkButton ID="LinkButton1" CommandName="select_category" CommandArgument='<%#Eval("CategoryID")%>' runat="server"><img src="<%#Eval("imageSource")%>" alt="category" /><br />
</div>
</ItemTemplate>
</asp:Repeater>
calling <%=SelectedID%> in the item template works but when i try the following expression the value of SelectedID returns empty..
<%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "match" : "not a match"%>
the value is being set as follows...
protected void categoriesCarouselRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
SelectedID = e.CommandArgument.ToString();
}
Any ideas whats wrong here?
Within the categoriesCarouselRepeater_ItemCommand code you've shown, you're assigning the CommandArgument to a property called 'SelectedCategory'.
Should this not be assigning the property to the 'SelectedID' property instead?
** EDIT..
The problem I see is one of two scenarios:
1) You are not rebinding the repeater with each postback, and therefore the expression within your ItemTemplate is not being evaluated - The output from the repeater will remain unchanged with each postback.
OR
2) You are rebinding the repeater control with each postback, however, upon clicking on your LinkButton for the first time, the repeater control is re-binded PRIOR to the ItemCommand event handler firing, and therefore, the 'SelectedID' property has not been set until after the repeater has finished being output.
If you were to click on one of your LinkButtons a 2nd time, the previously selected ID would be in viewstate at the time of the repeater control contents being rendered, and therefore be one step behind in rendering which category has been clicked, and so on...

Categories

Resources