I have a gridview with boundfields inside the grid. I'm trying to get the values of the boundfields when OnRowUpdating is fired. But when I try to read the new values the result is always empty.
This is the instruction I'm using :
protected void MyGridView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = MyGridView.Rows[e.RowIndex].Cells[3].Text;
DataBind();
}
Maybe I'm wrong, since I haven't used the GridView for a while, but shouldn't you be accessing the NewValues collection of e?
That's the beauty of the GridView: it keeps track of old vs. new values, and conveniently makes them available to you without you having to fish around to find them.
string id = (string) e.NewValues["whatever"];
TextBox sampleTextBox=((TextBox)MyGridView.Rows[e.RowIndex].FindControl("CostTextBox"));
string data=sampleTextBox.Text;
try this this, where CostTextBox is the id of the control you have given for the control in the grid.
Related
I have succeeded in retrieving Textbox from textbox column of Gridview, but it can't retain the value I have put there. It simply returns reference to textbox but miss the encapsulated value; Following is sample code I'm using to retrieve the textbox and its value; Please help me out of handling such problem.
protected void Submit_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowIndex = int.Parse(e.CommandArgument.ToString());
string userId = ApprovalParentGridView.DataKeys[RowIndex]["UserID"].ToString();
GridViewRow row = ApprovalParentGridView.Rows[RowIndex];
TextBox remarks = (TextBox)row.Cells[6].FindControl("txtRemark");
string remarks = remarks.Text.Trim();
...
}
Thanks in advance!!!
I have had this in the past and found it to be because the Gridview is loaded in Page_Load and the textbox had its value set in that routine, and Submit_RowCommand is fired afterwards.
You need to rebuild the gridview so that the control is in the controls collection but you need to be careful that you do not put the value in the textbox at the same time or else the code behind will overwrite the value that you user has entered from their browser.
I've got a problem with my DataGrid's Edit command.
Here's my DataGrid:
http://imgur.com/0nmDJX0
and now i would like to edit my Title:
http://imgur.com/VU20GNa
How can i get the "abc" value?
((TextBox) e.Item.Cells[0].Controls[0]).Text still gives me the old value "sdfsd".
How can i get that "abc" value from textbox? I store my records to XML so all i need is to get these edited values.
Thanks.
Make sure you are not binding your data on every page load. If you do that, your new values are reloaded with old values before your event handler runs.
In my case, I just needed to put the BindData() inside the if (!Page.IsPostBack) block
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
I am having trouble getting my datasource linked to my repeater through this code
protected void Page_Load(object sender, EventArgs e)
{
//HiddenField used as a placholder
HiddenField username = list.FindControl("username") as HiddenField;
//list is a DataList containing all of the user names
list.DataSource = Membership.GetAllUsers();
list.DataBind();
//Creates a string for each user name that is bound to the datalist
String user = username.Value;
//profilelist is a repeater containing all of the profile information
//Gets the profile of every member that is bound to the DataList
//Repeater is used to display tables of profile information for every user on
// the site in a single webform
profilelist.DataSource = Profile.GetProfile(user);
profilelist.DataBind();
}
I am getting the error message
An invalid data source is being used for profilelist. A valid data source must implement either IListSource or IEnumerable.
Well the reason why it will not work is because Profile.GetProfile returns ProfileCommon. As the error states the type you set profilelist.Datasource equal to, must be IListSource or IEnumerable.
I would suggest not using a repeater since you don't have actual repeating data to display.
EDIT
I think this is what you want to do.
IEnumerable<ProfileCommon> myProfileList = new IEnumerable<ProfileCommon>();
foreach(var user in userlist)
{
myProfileList.Add(Profile.GetProfile(user));
}
profilelist.datasource = myProfileList;
Your going about this wrong. As Etch said, a repeater is for lists of things. GetProfile doesn't return a list.
You're better off just putting your controls in a panel and assigning them in the "list" controls ondatabinding event.
In other words, you don't need a repeater here.
I forgot to post this up but for anyone that needs to do something similar here is the code behind that works
protected void Page_Load(object sender, EventArgs e)
{
List<MembershipUserCollection> usernamelist = new List<MembershipUserCollection>();
usernamelist.Add(Membership.GetAllUsers());
List<ProfileCommon> myProfileList = new List<ProfileCommon>();
foreach (MembershipUser user in usernamelist[0])
{
string username = user.ToString();
myProfileList.Add(Profile.GetProfile(username));
Label emailLabel = profilelist.FindControl("EmailLabel") as Label;
}
}
At the moment this is displaying about 15 user names and providing an ability to link to each of theses users respective profiles.
I have a gridview that has a list of Salesman names. When a Salesman is selected using A ButtonFieldTemplate in the Grid. I need the name of selected Salesman to appear in a Label.
So far I have this but it is not working:
protected void gvSalesmanByManager_SelectedIndexChanged(object sender, EventArgs e)
{
lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedValue + "'s Customers";
}
It's not throwing any errors. Not even red squiggly lines in Visual Studio. It's just plain not working.
How can I accomplish this in ASP.net and C# 4.0.
Regarding to the documentation SelectedValue gets the data key value of the selected row in a GridView control. Does your GridView contain a column with the key and did you set the dataKeyNamesProperty (such as datakeynames="myID")?
EDIT:
To access a value of a column you can use SelectedRow:
GridViewRow row = GridView1.SelectedRow;
lblSalesmanCustomers.Text = row.Cells[2].Text;
EDIT2:
There are two options when you want to read the template field. Either you store your value in an additional invisible column or you access the controls inside the template field. Something like this should work:
lblSalesmanCustomers.Text = ((TextBox)row.Cells[2].FindContol("tbxName")).Text;
Make sure you are not assigning empty text on page load event.
or you need to place those initialization in if(!Page.IsPostback) block
My code so far : protected void gvSalesmanByManager_SelectedIndexChanged(object sender, EventArgs e) { lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedValue + "'s Customers"; } –
try in the event "gvSalesmanByManager_SelectedIndexChanged" following code:
lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedRow.Cells[0].Text;
You said something about a "ButtonFieldTemplate" please show me some code from your ascx-file this will help us much i think.
*Edit:
"Controls[1]" has not to be right. But 0 is a literal. you could also debug your code and set breakpoints. would be much easier.
try this with Linkbutton:
LinkButton lnk1 = gvSalesmanByManager.SelectedRow.Cells[0].Controls[1] as LinkButton;
lblSalesmanCustomers.Text = lnk1.Text;
best regards,
noone
I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}