Is it possible to access element from inside DataList - c#

I am working on a car adverts website. Currently I am trying to do a simple wish list feature allowing the user to store certain cars to his wish list on button click. My cars are presented to the user via DataList. As there is only one button which is repeatedly created on every item from the list I wanted on click to check which record from the DB corresponds to the item where the button was clicked and then add it to another wish list DB.
So far I have come up with this:
<ItemTemplate>
carID:
<asp:Label ID="carIDLabel" runat="server" Visible="false"
Text='<%# Eval("carID") %>' />
<br />
<asp:Button ID="btn_wishList" runat="server" Text="Add to Wishlist"
onclick="btn_wishList_Click" />
<br />
</ItemTemplate>
And the button code:
//table adapters initialization ommited
foreach (DataRow row in carDetailsTable.Rows)
if (carIDLabel.Text == System.Convert.ToString(row["carID"]))
{
//DB insertion ommited
}
The problem I am encountering is that on the button code it says:
The name 'carIDLabel' does not exist in the current context.
Any suggestions how to access the Label's information or any other smarter ways to compare and then add to the wishDB ?

You can put the ID directly in the Button control as a command argument and handle the OnCommand event:
<asp:Button ID="btn_wishList" runat="server" Text="Add to Wishlist" CommandArgument="<%# Eval("carID") %>" OnCommand="btn_wishList_Command" />
Your event handler would be something like:
protected void btn_wishList_Command(object sender, CommandEventArgs e)
{
string id = (string)e.CommandArgument;
}

Related

How to retrieve the value of a ListView item in the same row as a LinkButton

I have a dynamically generated ListView which displays a set of groups, retrieved from a database. The ListView template looks like this:
<asp:ListView ID="lvGroups" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblGroupName" runat="server" Text='<%# Eval("GroupName") %>' />
</td>
<td>
<asp:LinkButton ID="lnkRemove" runat="server" Text="Remove" OnClick="lnkGroupRemove" OnClientClick="Confirm()" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
As you can see, there is a value, which is pulled from a database, and then a linkbutton for removing that value. When the linkbutton is clicked, it displays a javascript confirmation message, and if you click Yes on that message, then that specific entry will be removed from the database.
Unfortunately I cannot simply remove the GroupName where the ID = row number, because you can both add and remove groups, so that will quickly become inconsistent.
All I truly need is a way to retrieve the GroupName from the same row as the linkbutton that was clicked, if I can figure out how to do that, then I can easily configure a database query to successfully remove the entry. If you have another solution, however, that is also great.
I'm sad to say that I have no example code for the lnkGroupRemove event, as I simply have no clue where to start on this problem.
Any help on this subject would be much appreciated!
You can use the DataKeyNames attribute:
<asp:ListView ID="lvGroups" runat="server" DataKeyNames="ID,GroupName" >
and get the value this way in code-behind:
protected void lnkGroupRemove(object sender, EventArgs e)
{
ListViewItem item = (sender as LinkButton).NamingContainer as ListViewItem;
int ID = (int)lvGroups.DataKeys[item.DataItemIndex].Values["ID"];
string groupName = (string)lvGroups.DataKeys[item.DataItemIndex].Values["GroupName"];
}
I added an ID value to show that additional fields can be added to DataKeyNames and retrieved from code-behind, including fields that are in the data source but not displayed in the ListView.

redirecting to a url from a templatefeild in grid view

I have a grid view in my page in which i added a template feild.inserted a button which upon clicking will redirect the user to the actual request to make edits. The button that i added in the item template is bound to the data.The below is the code for the template feild
<asp:TemplateField HeaderText="RequestId" SortExpression="roc_id">
<EditItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("roc_id") %>'></asp:HyperLink>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text='<%# Bind("roc_id") %>' />
</ItemTemplate>
</asp:TemplateField>
how do i read the text from the botton click and redirect the user to the request that they click.I have listed the url below and i need to read the requestid at the end of url
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/Edit.asp?ROCID=
}
Thanks
You can use GridView.RowCommand to capture the button click event.
Also, you need to add a command name to the button in template field.
There is a good article about Respond to Button Events in a GridView Control which will help you

Bring up input form on same page with dropdownlist?

I'm brand new at this, using vs2010 with asp.net and c#, and I'm trying to use a button click event to display forms on my "Add Product" page (the forms will be textbox/label based for inputting data), using items from a dropdownlist of products. Which methods are available/is there a 'best practice' for this sort of thing? I've been fooling around with an if (Dropdownlist.SelectedIndexChanged) statement, but I'm not quite clear on why the syntax requires the SelectedIndexChanged method to preclude a += or -=. Thoughts?
The SelectedIndexChanged method has the += because you are adding an event handler to a specific elements event. Is the button you click on the Add Product page? If so for the OnClick event you could just set your form details based on the DropDownList SelectedItem or SelectedIndex. You may also need to wrap that panel in an UpdatePanel so that it can update visibility without reloading the whole page.
<asp:UpdatePanel ID="updateFormPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblName" runat="server" Text="" />
<asp:TextBox ID="txtDetails" runat="server" Text="" />
//More TextBox's or whatever you want
</ContentTemplate>
</asp:UpdatePanel?
<asp:DropDownList ID="ddlProductCategory" runat="server" />
<asp:Button ID="btnAddProduct" runat="server" OnClick="AddProduct_Click" Text="Add Product" />
//Code File Behind
protected void AddProduct_Click(Object sender, EventArgs e)
{
lblName.Text = ddlProductCategory.SelectedItem.Text;
txtDetails.Text = ddlProductCategory.SelectedItem.Value;
}

ASP.net ListView Access controls in ItemUpdating event

I'm missing something here...
I need to access the values of the controls on the item that is leaving editmode, thus firing ItemUpdating.
I found how to get the key value so I know which record in the database I have to update. The problem is that I can't seem to access the values of the controls on the editing row.
the EditTemplate contains a few checkboxes, a dropdown and textbox. I need to access these values so I can update the record.
When looking at the eventargs, nothing shows.
I think I'm overlooking something crucial here, any help would be handy.
You can also use "ListView.EditItem.FindControl("X")" to access control directly.
Protected Sub ListView_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView.ItemUpdating
Dim DropDownListddl As DropDownList = ListView.EditItem.FindControl("DropDownListddl")
Lblwarning.Text = DropDownListddl.SelectedValue
End Sub
There are two ways I can think to do this.
The first way would be to use a datasource that supports an update command and using two way binding to up date the values. The following snipped uses two way binding to populate the name and student fields and it will also update them for you.
<EditItemTemplate>
<tr style="background-color: #FFCC66;color: #000080;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
</td>
<td>
<asp:TextBox ID="studentTextBox" runat="server" Text='<%# Bind("student") %>' />
</td>
</tr>
</EditItemTemplate>
If you are not using a datasource that supports this you can do one other thing.
Notice how the update button has a command called "Update". You can use this to fetch the control values you want by handling the list views ItemCommand Event.
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextBox box = (TextBox)e.Item.FindControl("nameTextBox");
string name = box.Text;
}
}
You can find any control in the editied item by simply calling find control and passing it the control's ID, and don't forget the cast.
Hope this helps

How can i access a control within a ListView once a button has been clicked?

I need to access a label control in a listview when I've clicked a button (that is on the same row)...
Does anyone know how to do this please? :(
See below for more of an insight...
ASPX Page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
Code Behind:
protected void voteOnThis(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Voting.vote(int.Parse(myButton.CommandArgument));
// Here i would like to access the 'label' lblDone and make this Visible
}
In this simple case, I should consider using Javascript (JQuery)
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" style="visibility:hidden">Your vote has been counted</asp:Label>
<asp:Button OnClientClick="showLblDone()" ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
now, define inside a script tag the showLblDone function:
<script>
function showLblDone (){
$(this).siblings('span').show();}
</script>
You can also call this function with a parameter if you want to show/hide on every click, or you can use .toggle() instead of .show().
In this case you must add a div (or a Panel) inside the ItemTemplate.
You need to hook into the listview row bind and add the information you want to have when clicked. Using this, you can add an attribute to the button that you read back on click, for example...
If you posted some actual code, I could probably help some more.

Categories