I have a GView and I am populating rows from code behind. However, in the RowUpdating event I want to access the changes done by the user and store that change into a string. Here is my code:
protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
System.Web.UI.WebControls.TextBox myBox = gvShowComm.Rows[e.RowIndex].FindControl("PlanName") as System.Web.UI.WebControls.TextBox;
string s = myBox.Text;
gvShowComm.DataBind();
}
string s is still showing me the OLD text which is populated before. I want to store new string which user entered. How do I do that?
make sure you repopulate rows and call DataBind at least on PageLoad, preferably on OnIniti without doing that, you always get values from viewstate (if you didnt disable it). at a last resort, you can investigate Request.Form and find if you recieved new value properly
Can't you use e.NewValues[] . The result that you are getting is probably e.OldValues[]
Related
I have a SqlDataSource bound to a GridView with an SQL that takes about 10 seconds to retreive the data.
Also there is a UserControl called "PageSizeControl" which hooks the selected-event of the GridView's SqlDataSource. In this event, I need the DataTable to prepare some settings of the PageSizeControl.
Currently, I'm doing this with following code:
protected void ds_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
SqlDataSourceView dsv = (sender as SqlDataSourceView);
dsv.Selected -= ds_Selected;
DataTable dt = (dsv.Select(DataSourceSelectArguments.Empty) as DataView).Table;
int iRowCount = dt.Rows.Count;
// some gui-adaption like visibility, text, ...
}
In older versions we used e.AffectedRows. But the value stored in e.AffectedRows is not correct when a Filter is applied to the DataSource. And we have use-cases where we don't only need the row count but the whole DataTable.
The problem is, that the .Select() re-executes the Db-Query and this takes another 10s to finish.
I also tried to turn caching on the SqlDataSource on:
EnableCaching="true" CacheDuration="Infinite"
But this wasn't helpful in two reasons:
1. The OnSelected event is not fired when cached data get accessed
2. If the OnSelected event get's fired (because data wasn't yet cached), the .Select() is still executing uncached and takes 10s.
Does anybody have clues how I can get the data without a time-consumpting re-execution of the query? Best would be in the OnSelected, but I'm open for another suggestions.
I got a workaround running that fits my requirements. I use the event GridView.OnRowDataBound and get the DataItem of the first GridRow, which contains the DataTable.
private DataTable oData = null;
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (oData == null && e.Row.DataItem != null)
{
oData = (e.Row.DataItem as DataRowView).Row.Table;
}
}
This solution works, but it looks quite dirty and it requires a GridView (which in my case is no problem). I would be grateful for a more clean solution.
UPDATE
After a longer research with IlSpy I got to the conclusion that it is not possible to get the data in the OnSelected event. Even not if caching is enabled, since cache is written after OnSelected.
So the easiest way is to turn cache on and call the SqlDataSource.Select(...) function where you need the data.
Another way is to get the data by yourself with SqlDataSource.Select(...) and then bind the table it to the controls. But this has some disadvantages. For example: sorting and paging on GridView doesn't work out of the box when bound to a dataset/datatable.
And yet another way is to extract the data from the control which selected it. See above for an example for GridView.
I've been having a tough time with updating my text boxes and could really use some help. So basically, I have a popup panel for the user to select a value that I then populate into a certain text box.
I am certain that the problem is either very simple to solve, or that I'm confused on what post back actually does. Here is a snippit of my code below:
protected void grvSearchRecords_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (string.Compare(e.CommandName, "EditRow", true) == 0)
{
long nMagic2Value = Convert.ToInt64(e.CommandArgument);
string tmp = GetItem(nMagic2Value, currentTableName);
textBox1.Text = tmp;
Debug.WriteLine(textBox1.Text.ToString());
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SearchRecords", "$(document).ready(function(){ $('#mask, #divSearchRecordsGrid').fadeOut(\"fast\"); });", true);
}
}
The "tmp" variable grabs the value I need and assigns it to the text box.
I then run a Debug statement and confirm that the value is correctly assigned to the box. As soon as the function ends, however, that assigned value is lost, and the text box never populates with the new value.
Wrap your code around a post back check
if (!IsPostBack)
{
//your code here
}
This will ensure that your code does not run when it is a post back, so your textBox will not be clear.
Post back means that the page is not being rendered for the first time, for example a page refresh.
Okay, I just got it working. The textbox I was trying to change was part of a Content tag and nothing else. I enclosed it in an UpdatePanel and added a trigger for my GridView, and now it works!
The joys of learning a new language.
During development with Devexpress, we've encountered the use of a InstantFeedbackSource. Using the Row_Loaded, we would like to adjust values of certain rows. Yet the CellContent isn't changed.
Testsolution:
GetQueryable-Method of my datasource:
private void linqInstantFeedbackSource1_GetQueryable_1(object sender, GetQueryableEventArgs e)
{
e.QueryableSource = _testObjects.AsQueryable();
gridView2.RefreshData();
gridControl2.RefreshDataSource();
}
My Row-loaded event
private void gridView2_RowLoaded(object sender, DevExpress.XtraGrid.Views.Base.RowEventArgs e)
gridView2.SetRowCellValue(e.RowHandle, gridView2.Columns["RandomGuid4"], "lalalala");
}
Information:
gridView2.Columns["RandomGuid4"] is not null and refers to the correct-column.
e.RowHandle Refers to the correct Linenumber
Goal:
I want to achieve the following behavior:
At runtime i want to fill the gird with records. When adding the rows i want to check the row for certain values. When a certain value occurs then several another cells should be filled with combobox, hyperlinks,...
The advised way by devexpress is the event CustomCellEdit. But that event is highly unreliable. We're experiencing issues (exceptions from within the control). I've submitted several tickets so far but no response. So we're searching an alternative.
Could someone point out what i'm exactly missing/doing wrong here?
Thank you for your time.
Note: I've tried the use of RefreshData in my RowLoaded-Event but it doesn't appear to be working.
I've got a question I've got a RadListView which I populate with a custommade list of ListViewDataItems (all under WinForms).
this.listView.Items.Add(new ListViewDataItem(myCustomId, new string[] { fileName, fileSizeInMB});
Then I added a doublclick event:
listView.DoubleClick += mainFormListView_DoubleClick;
And declared the event:
void listView_DoubleClick(object sender, EventArgs eventArgs)
I then access the currently selected item with listView.Items[listView.SelectedIndex].
So far so good.
But when I try to access the data I had put into that Item the problems start.
In total I want to get the "myCustomId" of the selected item when an item is doubleclicked,
but all I manage to get is a ListViewDataItem that holds not a single Data, and only contains format properties.
Can anyone tell me what I'm doing wrong there or how exactly I can access the previously stored ID?
thanks
When you created the ListViewDataItem, you set myCustomId as the Text property.
... new ListViewDataItem(myCustomId, ...
So see if the Text property on the SelectedItem has the value you're looking for.
private void radListView1_DoubleClick(object sender, EventArgs e)
{
var myCustomId = radListView1.SelectedItem.Text;
}
You may also want to test radListView1.SelectedItem to make sure it's not null before trying to access the Text property.
From your comment:
the Debugger still says that neither function exists (only format functions when I get the mouse over "listView.SelectedItem" on the line var a = listView.SelectedItem
When a place a break point at runtime and inspect SelectedItem, I see both properties have a value:
I am attempting to insert an items using a basic FormView control.
I am somewhat new to C# and its controls, so bear with me.
The FormView has standard entries like:
ItemName
ItemPrice
ItemSize
It also has controls that will be hidden from the user such as:
ItemDateCreated
ItemDatechanged
ItemChangedBy
These items I am attempting to modify their values before the Insert() takes place, so I have captured the event InsertButton_Click():
protected void InsertButton_Click(object sender, EventArgs e)
{
LinkButton btnInsert = (LinkButton)FormView1.FindControl("InsertButton");
TextBox txtDateAdded = (TextBox)FormView1.FindControl("ItemDateAddedTextBox");
TextBox txtDateChanged = (TextBox)FormView1.FindControl("ItemDateChangedTextBox");
TextBox txtChangedBy = (TextBox)FormView1.FindControl("ItemChangedByTextBox");
txtDateAdded.Text = DateTime.Now.ToString("MMMM dd, yyyy");
txtDateChanged.Text = DateTime.Now.ToString("MMMM dd, yyyy");
txtChangedBy.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();
tblItems.Insert();
}
It keeps telling me that the ItemName field is NULL, and throws an error, even though I can plainly see the value is being set in the textbox. Why is this value being thrown as NULL? Do I need to manually create the INSERT statement before I call the Insert()? How would I go about doing that?
Without seeing your .aspx code it's hard to tell for sure, but ItemName wasn't included in the code sample you gave. It sounds like your databinding syntax might not be correct, so the value in the textbox on your page isn't getting correctly mapped to the parameter in your Insert command.
Also, what you probably want to do instead of having hidden textbox controls is tie into the Inserting event on your datasource. This example assumes you're using a SqlDataSource control, so change according to your specific datasource:
private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters.Add(new SqlParameter("ItemDateAddedTextBox", DateTime.Now.ToString("MMMM dd, yyyy")));
...
}
You attach this handler in the definition of your datasource in your .aspx page. In the design view of your webpage, click on the datasource and in the properties window, click over to the Events tab. If you double-click the Inserting event, it should create the handler for you in your codebehind, and you just fill in your implementation.
If the field values are set from system then I would recommend doing that directly in Sql using getdate and not via asp.net code.
e.g.:
insert into mytable (...,..., ItemDateCreated) values (...,...,getdate())