When handling the OnUpdateCommand event on a RadGrid the DataItem is null.
I thought that this would also represent the data item being represented by the row.
The Radgrid is populated from an IList and in the handler the code looks like this...
protected void rgAllocatedClients_UpdateCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridDataItem)
{
var gridDataItem = e.Item as GridDataItem;
var client= gridDataItem .DataItem as Client;
....
....
This works find when handling the ItemDataBound event but not when handling the UpdateCommand event. I really need this as in my Client class is the Id of the row I want to handle the update for.
Thanks,
Assuming your Grid is in Edit mode befor ethe Update Command, you should cast e.Item to GridEditableItem instead of GridDataItem
Try this by using GridEditableItem
protected void grdContacts_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
string idEditing = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
GridEditableItem editedItem = e.Item as GridEditableItem;
Hashtable newValues = new Hashtable();
// ur code
}
Related
Hi,Here i am trying to implement the linkButton click event in a
gridview through code behind using the BoundField Class in gridview.
when i trying to add the individual BoundField values Directly to grid
as a column in page_Load and binding the linkbutton in
RowDataBoundEvent of the row of cell with linkbutton click event, it
is firing the linkButton Click event well with the following code.
protected void Page_Load(object sender,EventArgs e)
{
BoundField bfield = new BoundField();
bfield.HeaderText = "EmpName";
bfield.DataField = "EmpName";
gridView.Columns.Add(bfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "Monday";
bfield1.DataField = "Monday";
gridView.Columns.Add(bfield1);
}
and in on RowDataBound Event i have wrote
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkViews = new LinkButton();
lnkViews.ID = "lnkViews";
lnkViews.Text = (e.Row.DataItem as DataRowView).Row["Monday"].ToString();
lnkViews.Click += new EventArgs(linkButton_ClickAction);
e.Row.Cells[2].Controls.Add(lnkViews);
}
}
protected void linkButton_ClickAction(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = row.Cells[0].Text; this.ModalPopupExtender1.Show();
}
But when i trying to add the above BoundFields by looping based on the
table columns count like this, in page_Load event, the event is not
firing.
protected void Page_Load(object sender,EventArgs e)
{
DataTable dtTable = new DataTable();
dtTable= BindGrid();
BoundField boundField;
for (int i = 0; i < dtTable.Columns.Count; i++)
{
string ColumnValue = dtTable.Columns[i].ColumnName;
boundField = new BoundField();
boundField.HeaderText = ColumnValue;
boundField.DataField = ColumnValue;
gridView.Columns.Add(boundField);
}
}
when we creating the BoudnField event using the above code in a loop
based on dataSource columns count, it doesn't firing linkbutton event. why?
as per my understanding you are trying to form grid dynamically.
One thing is bound fields will not support for bubbled events. So better replace them with template fields that will meets your requirement.
You need to assign click event in InstantiateIn method by defining ITemplate interface.
OnRowDataBound event doesn't comes into picture.
below is example for your reference.
http://forums.asp.net/t/1001702.aspx
I am using a telerik RadGrid that is being populated via a ObjectDataSouce. This object returns a series of boolean fields
<telerik:GridCheckBoxColumn DataField="IsSysAdmin" DataType="System.Boolean"FilterControlAltText="Filter IsSysAdmin column" HeaderText="Sys Admin"
SortExpression="IsSysAdmin" UniqueName="IsSysAdmin">
Once I select a column I would like to be able to derive the boolean value for use in another section of the page.
I can get at the values in the selected rows by doing the following:
protected void gv_roleList_Command(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
GridDataItem item = (GridDataItem)e.Item;
item.Selected = true;
txt_RoleName.Text = item["RoleName"].Text;
...edited for brevity
By calling the column I can get the values however this does not work for a GridCheckBoxColumn the text attribute only returns (which I would expect).
I have tired to cast the sender as checkbox to go at it that way but my implementation does not seem to work.
var cb = (GridCheckBoxColumn)sender;
Does anyone have any tips on how to go about deriving the value from the Checkbox column?
Cheers
Please check below code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
// if current row is in normal mode
GridDataItem item = e.Item as GridDataItem;
CheckBox chk = item["IsSysAdmin"].Controls[0] as CheckBox;
// If your row is in edit mode
GridEditableItem eitem = e.Item as GridEditableItem;
CheckBox echk = eitem["IsSysAdmin"].Controls[0] as CheckBox;
}
}
I'm working on asp.net website with Vb.net and I have a dropdownlist with autopostback = true and I need to get the selected value when I change the item or I want to get the item which fires the selectedindexchanged event ..
any help please..
In ie. your Page_Load set
this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);
Then write the event handler like this:
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string selected = (string) comboBox.SelectedItem;
}
Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:
if (Page.IsPostBack)
return;
try this:
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
string value = (string)list.SelectedValue;
}
If item is a Dictionary:
string value = ((KeyValuePair<string, string>)combobox.SelectedItem).Key;
I'm using XmlDataSource as the datasource for a dropdownlist.
Now I want to set the SelectedValue of the drop down when the page initially loads. I have tried the OnDataBound event of the drop down in which I could see the total items. But setting the SelectedValue didn't work. InOnDataBinding event, I couldn't even see the total items probably because the list isn't bound yet?
How can I set the selected index based on a value?
This seems to work for me.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}
DropDownList1.Items.FindByValue(stringValue).Selected = true;
should work.
This is working code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataTextField = "user_name";
DropDownList1.DataValueField = "user_id";
DropDownList1.DataSource = getData();// get the data into the list you can set it
DropDownList1.DataBind();
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("your default selected text"));
}
}
Have you tried, after calling DataBind on your DropDownList, to do something like ddl.SelectedIndex = 0 ?
I have several DropDownLists on a form which are dynamically populated as they move down the form pulling data from a DB. The data is all HTMLEncoded so I need to HTMLDecode the data to display the text.
I created a method to do this and trigger it 'ondatabound' for each DDL
ondatabound="SortHTMLModel"
BUT whats annoying I have the same method just changing the DDL name on each one. I want a generic single method each DDL could call. Here is the one for the DDL called ddlfuel
protected void SortHTML(object sender, EventArgs e)
{
foreach (ListItem item in ddlFuel.Items)
{
item.Text = Server.HtmlDecode(item.Text);
}
}
And one for the DDL called ddlModel
protected void SortHTMLModel(object sender, EventArgs e)
{
foreach (ListItem item in ddlModel.Items)
{
item.Text = Server.HtmlDecode(item.Text);
}
}
You see my predicament! So annoying I just can't figure out the syntax for one method
IIRC, the sender of an event is the actual control, so you could also say
protected void SortHTML(object sender, EventArgs e)
{
foreach (ListItem item in ((DropDownList)sender).Items)
{
item.Text = Server.HtmlDecode(item.Text);
}
}
and bind each DropDownList's DataBound event to SortHTML
Why can you not subclass the DropDownList control to do that before it renders the control? Then instead of using the stock DropDownList, you use your subclassed dropdownlist and the functionality happens automatically.