I have a dropdownlist control in a datagrid template field. In the SelectedIndexChanged event, I just want to get the position of the sender object in order to create a reference to the row the sender object was in. All I've found on google is how to loop through each row of the datagrid to compare it to sender's client ID to see if it is in fact my selected row. Why can't I just get the position of the sender object and just use it to create an instance of that gridviewrow? And why doesn't the below work?
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.SelectedRow;
var store = row.Cells[0].Text; //I get the Object reference not set to an instance of an object error here
}
Am I missing something?
The sender is the DropDownList. Its NamingContainer is the GridViewRow. This has a property RowIndex. I assume that this (or the row) is what you want. Here is both:
var row = (GridViewRow)((Control)sender).NamingContainer;
var rowIndex = row.RowIndex;
Related
I am trying to change a cell value in a GridView when a user edits another cell value in that GridView. Here is the RowUpdating function that I have tried:
Private Sub Gv_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles Gv.RowUpdating
Dim Gvr As GridViewRow = DirectCast(sender.NamingContainer, GridViewRow)
Dim lessonName As String = Gvr.Cells(2).Text
Gvr.Cells(3).Text = GetLessonID(lessonName)
End Sub
As you can see, I tried casting the naming container of the sender to a GridViewRow, but that doesn't work. I thought the naming container would be the GridView itself, but it is not. Basically, I need to know how to access the cell that needs to be automatically changed in the RowUpdating function. I should mention that the cell in question cannot be edited by the user; rather, it is updated automatically when the Update button is clicked. Any help is greatly appreciated. Please let me know if I can clarify anything. Thanks!
Leverage the RowIndex Property from the GridViewUpdateEventArgs parameter and access the grid directly:
gv.Rows(e.RowIndex).Cells(3).Text = grv.Rows(e.RowIndex).Cells(2).Text
I have 3 controls on my web page, a dropdownlist, a detailsview, and a listview.
the dropdownlist controls the record to be displayed on the detailsview and the listview.
when the detailsview enters "new" mode I clear the items of the detailsview and clear the values of the dropdownlist:
If e.CommandName = "New" Then
lvRecipeSteps.Items.Clear()
ddRecipeItemNumber.Items.Clear()
End If
this shows the page with no items on the dropdownlist, the detailsview and listview on new/insert mode.
the issue happens when I click the cancel event, then this code is executed
If e.CommandName = "Cancel" Then
ddRecipeItemNumber.DataBind()
ddRecipeItemNumber.SelectedValue = Session("Id").ToString()
End If
the session variable Id gets assign on page load
Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Session("Id") = ddRecipeItemNumber.SelectedValue
I get an error: "not set to an instance of an object" pointing to: ddRecipeItemNumber.SelectedValue = Session("Id").ToString()
I want to be able to save the record Id, that is on the dropdown before the new/insert event begins, and then if the event gets cancelled, set the controls back to the record that were displaying before the insert/new action started.
I read that using sessions variables is a good way of doing this, but I most be doing something wrong that when the event cancel is executed, the values is then lost.
Please let me know if I can get some help finding a solution for my problem.
Thank you very much.
place the Session("Id") = ddRecipeItemNumber.SelectedValue inside the condition below:
if (!IsPostBack)
{
Session("Id") = ddRecipeItemNumber.SelectedValue;
}
just need to convert it to VB
UPDATE 1
If your code is not inside a Data control then you could allocate its selected value like the code below:
on your aspx form:
<asp:DropDownList ID="DropDownList2" runat="server"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
AutoPostBack="true" ></asp:DropDownList>
on your code behind:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Id"] = DropDownList2.SelectedItem.Value;
}
UPDATE 2
also if you want to change the selected value on your code this would be the right code to do:
instead of this line ddRecipeItemNumber.SelectedValue = Session("Id").ToString()
use this one
ddRecipeItemNumber.Items.FindByValue(Session("Id").ToString()).Selected = true;
I am creating a website on Visual studio 2010. i am creating it on asp.net with Visual Basic coding. I am a university student and am a beginner with this.
Basically i created a sqlDataSource and a Grid View and linked them together with drop down boxes so that the user can refine their table results. On the grid viewer i also turned on the 'select' feature so that the user can select a row.
What i am struggling to do is to retrieve at least one of the cells of the selected row in the grid view and put it into a textbox on the same page. The main cell i want to retrive is the ID so that i can use later.
Please could you help me find a suitable solution for this.
Thank you
This is in C# but the event is the same that you will target. When the user selects a row, this event will be fired and it will retrieve the selected row. Once you have the selected row you could get any column value of that row by using the index of the column.
void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridView1 row = GridView1.SelectedRow;
// You could access any cell in the row by doing row.cells(index)
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
Hope that helps!
EDIT
VB
Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Get the currently selected row using the SelectedRow property.
Dim row As GridViewRow = GridView1.SelectedRow
MessageLabel.Text = "You selected " & row.Cells(2).Text & "."
End Sub
Is it at all possible within a ListView ItemDataBound event handler to gain access to the full DataRow for that event? I need to do a lot of processing for the entire row on binding, but using data item values in the datarow that I am not actually using in the display itself.
Try this
DataRowView dr = (DataRowView)DataBinder.GetDataItem(e.Item);
using
dr.Item.ItemArray you can access the entire row.
Perhaps try to use the ListViewDataItem property to access the properties of the underlying data object to which the object is bound. The ListViewDataItem property is only available during and after the ItemDataBound events of the control and usually corresponds to a record in your data source object.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem.aspx
Below is an example.
protected void listProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string prodtype = (string)DataBinder.Eval(dataItem, "ProductType");
// ...
}
}
I have a ListView control and I have added a DataBound event (don't know if this is the correct one) to the control.
I'm wanting to access the data being bound to that particular ItemTemplate from this event, is that possible?
C# Solution
protected void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
// you would use your actual data item type here, not "object"
object o = (object)dataItem.DataItem;
}
}
Why they made this so different for ListView still sort of puzzles me. There must be a reason though.
A little late, but I'll try to answer your question, as I had the same problem and found a solution. You have to cast Item property of the ListViewItemEventArgs to a ListViewDataItem, and then you can access the DataItem property of that object, like this:
Private Sub listView_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles productsList.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dataItem As Object = DirectCast(e.Item, ListViewDataItem).DataItem
...
End Sub
You could then cast the dataItem object to whatever type your bound object was. This is different from how other databound controls like the repeater work, where the DataItem is a property on the event args for the DataBound method.
Found a workaround, I created a method to format the data how I needed and called it from the markup using:
<%# doFormatting(Convert.ToInt32(Eval("Points")))%>
The data that is used for the current item can be found from the EventArgs.
So from the RepeaterItemEventArgs e we can access the current item by looking in e.Item.DataItem.
protected void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var currentItem = e.Item.DataItem;
}
}