Radio list with SelectedIndexChanged not firing inside radgrid - asp.net

I have a user control that contains a radio list that on SelectIndexChanged it updates a drop down.
I put together a basic page and add the user control to the page it works fine but when I move the control to inside a radgrid it doesn't work, it will post back but never call the SelectIndexChanged event.
I've pulled up 2 previous questions on this Q. 1 and Q. 2 which say that OnSelectedIndexChanged needed to be set in the aspx page. My issue is that the control doesn't exist in the aspx page and is created later so that solution does not work for me.
Working code
working.aspx
<TT:ToolTipControl ID="ToolTipEdit" runat="server" />
working.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ToolTipEdit.getEditToolTip("POL_TERM_CD", "DataPolTermDropDownlistEdit");
}
User Control
userControl.ascx.cs
public void getEditToolTip(string fieldName, string ddlName)
{
DataPolTermRadioListBox ccPolTermRadioListBox = new DataPolTermRadioListBox(); //custom radio list
ccPolTermRadioListBox.ID = "PolTermRadioListBox";
ccPolTermRadioListBox.AutoPostBack = true;
ccPolTermRadioListBox.SelectedIndexChanged += new System.EventHandler(updateParent);
ToolTip.Controls.Add(ccPolTermRadioListBox);
}
Broken Code
brokenPage.aspx
<telerik:RadGrid ID="rgState" Skin="WebBlue" runat="server" OnNeedDataSource="rgState_NeedDataSource"
AutoGenerateColumns="False" OnPreRender="rgState_PreRender">
<MasterTableView DataKeyNames="wrtnStPolId" AllowAutomaticUpdates="false" AllowAutomaticDeletes="true"
AllowAutomaticInserts="false" CommandItemDisplay="Top" AllowMultiColumnSorting="True"
EditMode="InPlace" GroupLoadMode="Server" Caption="State(s) and Exposure(s)">
<Columns>
<telerik:GridTemplateColumn AllowFiltering="false" HeaderText="Pol Type Nstd" SortExpression="nonStdPolTypeCd"
UniqueName="nonStdPolTypeCd">
<ItemTemplate>
<asp:Label ID="lblNonStdPolTypeCd" runat="server" align="center" Text='<%#DataBinder.Eval(Container.DataItem, "nonStdPolTypeCd")%>' />
</ItemTemplate>
<EditItemTemplate>
<cc1:DataNonStdTypeCdDropDownList ID="ddlNonStdTypeCd" runat="server" ClientIDMode="Predictable">
</cc1:DataNonStdTypeCdDropDownList>
<TT:ToolTipControl ID="ttcNonStdPolTypeCdEdit" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
brokenPage.aspx.cs
protected void rgState_PreRender(object sender, EventArgs e)
{
RadGrid rgExpMod = (RadGrid)sender;
foreach (GridDataItem row in rgExpMod.Items)
{
GridDataItem gdiItem = (GridDataItem)row;
if (row.FindControl("ttcNonStdPolTypeCdEdit") != null)
{
DropDownList ddl = (DropDownList)row.FindControl("ddlNonStdTypeCd");
ddl.ID += row.RowIndex;
ddl.SelectedIndex = 2;
NCCI.PDC.Web.Controls.ucToolTip ttcNonStdPolTypeCdEdit = (NCCI.PDC.Web.Controls.ucToolTip)row.FindControl("ttcNonStdPolTypeCdEdit");
ttcNonStdPolTypeCdEdit.getEditToolTip("non_std_pol_type_cd", ddl.ID);
}
}
}

Related

itemTemplate item id not existing in code behind

I am trying to create textboxes that are equal to the number of rows in grid view (databound from db). here is my markup
<asp:GridView ID="quizGrid" runat="server" CssClass="Grid" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="admissionNO" HeaderText="Admission NO"/>
<asp:BoundField DataField="studentName" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Textbox runat="server" ID="marks" > </asp:Textbox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
but when i use the marks in code behind it says
quizGrid_marks_0 does not exists in the current context
what im doing wrong here?
You can't access your textbox like that in code behind file, rather you need to find them in RowDataBound event like this:-
protected void quizGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox marks = (TextBox)e.Row.FindControl("marks");
txtMarks.Text = "Test";
}
}
Edit:
Okay, suppose you have a button btnGetData with button click event as btnGetData_Click, then you can find the textbox text by looping through the gridview rows like this:-
protected void btnGetData_Click(object sender, EventArgs e)
{
GridView quizGrid = (GridView)Page.FindControl("quizGrid");
foreach (GridViewRow row in quizGrid.Rows)
{
TextBox marks = (TextBox)row.FindControl("marks");
}
}

ASP.NET - Finding Label Control in a GridView

I'm having a problem trying to find a label control that is inside a GridView.
Please see my codes below:
<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtDate" MaxLength="10" Width="70" />
<asp:ImageButton ID="imgScoreDate" runat="server" ImageUrl="~/images/calendar.gif" />
<ajaxtoolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" Format="MM/dd/yyyy" TargetControlID="txtDate" PopupButtonID="imgDate" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is my .cs file:
protected void LoadGridView()
{
//Do something else
foreach (GridViewRow row in MyGridView.Rows)
{
//Tried A
System.Web.UI.WebControls.Label lblName = row.FindControl("lblName") as System.Web.UI.WebControls.Label;
lblName.Text = "Name";
//Tried B
((System.Web.UI.WebControls.Label)row.FindControl("lblName")).Text = "Name";
}
}
I debug this code and it seems to work fine because my breakpoint is being hit each time the debugger runs. It even loops through my foreach block the same count as to how many rows my GridView has.
But I don't understand why my lblName control doesn't get the "Name" text as a value? Am I missing anything here? I tried both //Tried A and //Tried B methods but they both doesn't update my label's text.
Any help would be appreciated!
Thanks! Cheers!
You want to call LoadGridView inside PreRender. Basically, you want to call it after GridView is bound with data.
protected void Page_PreRender(object sender, EventArgs e)
{
LoadGridView();
}
Look at PreRender event of ASP.NET Page Life Cycle.
On your gridview add:
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
What I think is happening is the control is not recreated server side in its current spot.
try this
on .aspx page
<asp:GridView ID="MyGridView" runat="server"
onrowdatabound="MyGridView_RowDataBound" .../>
code behind ::
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGridView();
}
}
void LoadGridView()
{
DataTable dt = new DataTable();
// dt= call ur database method to get data
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Name = (Label)e.Row.FindControl("lblName");
lbl_Name.Text = "Name";
}
}
cheers!

Always getting checkbox as false in grid

i have checkboxes in a grid. I am trying to access them from codebehind and get the data for checked /unchecked rows.But even after the checkboxes being checked I am getting them as Checked property as false only:
Aspx:
<table width="100%">
<asp:GridView ID="grdRequestsPending" runat="server" Width="100%" AutoGenerateColumns="false"
BorderWidth="1px" BorderStyle="Solid" Style="margin-left: 0px" BorderColor="#ffcc00"
RowStyle-BorderColor="#ffcc00" RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px"
GridLines="Both" DataKeyNames="ReqID,ApproverComments" On="grdRequestsPending_ItemDataBound" OnRowDataBound="grdRequestsPending_RowDataBound"
OnPreRender="grdRequestsPending_PreRender">
<RowStyle CssClass="dbGrid_Table_row" />
<HeaderStyle CssClass="dbGrid_Table_Header" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblSelect" Text="Select All" runat="server"></asp:Label><br />
<asp:CheckBox ID="SelectAll" onclick="javascript:checkAllBoxes(this);" TextAlign="Left"
runat="server" />
</HeaderTemplate>
<ItemStyle Width="2%" />
<ItemTemplate>
<asp:CheckBox ID="chkReq" runat="server"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="7%" />
</asp:TemplateField>
</Columns>
But when I am checking these I am always getting them as false in code behind:
protected void UpdateVMRequestStatusByCapSupLead(int StatusId)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("ReqId", typeof(int));
dt.Columns.Add("StatusId", typeof(int));
dt.Columns.Add("ModifiedBy", typeof(string));
dt.Columns.Add("ModifiedDate", typeof(string));
dt.Columns.Add("txtCommentSupLead", typeof(string));
foreach (GridViewRow gr in grdRequestsPending.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("chkReq");
if (chk.Checked)
{
strReqId = strReqId + grdRequestsPending.DataKeys[gr.RowIndex].Value.ToString() + ',';
TextBox txtCommentSupLead = (TextBox)gr.FindControl("txtCommentSupLead");
dt.Rows.Add(dt.NewRow());
dt.Rows[dt.Rows.Count - 1]["ReqId"] = Convert.ToInt32(grdRequestsPending.DataKeys[gr.RowIndex].Value);
dt.Rows[dt.Rows.Count - 1]["StatusId"] = StatusId;
dt.Rows[dt.Rows.Count - 1]["ModifiedBy"] = Session["UserAccentureID"].ToString();
dt.Rows[dt.Rows.Count - 1]["txtCommentSupLead"] = txtCommentSupLead.Text;
dt.Rows[dt.Rows.Count - 1].AcceptChanges();
dt.Rows[dt.Rows.Count - 1].SetModified();
}
}
I am not getting the problem.I am gettign the control also correctly..
I assume that you're always databinding your GridView and not only if(!Page.IsPostBack)....
So put this in page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBindControls(); // like GridView etc.
}
}
If you DataBind controls they will lose their changes and ViewState. Even events aren't triggered then. So you should do that only on the first load if EnableViewState="true".
Here is a simple example of populating a checkbox in a gridview and getting the values out on a button click
Default.aspx
<asp:GridView ID="gv"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkReq" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSubmit"
runat="server"
OnClick="btnSubmit_Click"
Text="Submit" />
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Create 20 rows
gv.DataSource = Enumerable.Range(1, 20);
gv.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
var isCheckedList = new List<bool>();
for(var index = 0; index < gv.Rows.Count; ++index)
{
var chkReq = (CheckBox)gv.Rows[index].FindControl("chkReq");
isCheckedList.Add(chkReq.Checked);
}
//Look at isCheckedList to get a list of current values.
System.Diagnostics.Debugger.Break();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var index = e.Row.RowIndex;
//Strongly Bind Controls
var chkReq = (CheckBox)e.Row.FindControl("chkReq");
chkReq.Text = "Item " + index.ToString();
}
}
Since you did not show all of your code, it could be an issue like Tim Schmelter said or it could be something else. This example provides you with the basics that are needed to retrieve a checkbox value from a grid view.

How to redirect to two different pages in radgrid using two command buttons inside radgrid?

I have a radgrid1 and inside radgrid Item template i have two asp.net imagebutton namely imagebutton1 and imagebutton2 ....
i want when i click on Selected radgrid Item whose id is id then after clicking on Image Button1 i redirect to ~/book.aspx?id=1
and if i click on imagebutton2 then i redirect to ~/details.aspx?id=1
Note : the id of the item will be changed dynamically according to the selected radgrid row .
I have already done it using simple Gridview but m unable to perform this action using radgrid.
Help me please guys...!
Please check below code snippet.
.aspx
<MasterTableView DataKeyNames="Id"
>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="BookPage"/>
<asp:ImageButton ID="ImageButton2" runat="server" CommandName="DetailPage"/>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
.asp.cs
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
GridDataItem item = e.Item as GridDataItem;
string strId = item.GetDataKeyValue("Id").ToString();
if (e.CommandName == "BookPage")
{
Response.Redirect("~/book.aspx?id=" + strId);
}
else if (e.CommandName == "DetailPage")
{
Response.Redirect("~/details.aspx?id=" + strId);
}
}

How to access telerik controls from telerik radgrid

How to access telerik control that is RadAsyncUpload from below radgrid. I have below code in aspx page. During page load I need to disable telerik controls on some condition. How can I disable telerik controls from below code?
<telerik:RadGrid ID="RadGrid1"
runat="server"
AutoGenerateColumns="False"
GridLines="None"
Skin="Black"
Width="750px"
Height="320px">
<PagerStyle Mode="NextPrevAndNumeric" />
<SelectedItemStyle CssClass="SelectedItem"/>
<MasterTableView EditMode="InPlace"
CommandItemDisplay="None"
AllowFilteringByColumn="True"
DataKeyNames="FileName">
<Columns>
<telerik:GridBoundColumn ReadOnly="true"
DataField="FileName"
UniqueName="FileName"
AllowFiltering="false"
ItemStyle-Width="200px"
HeaderStyle-Width="205px"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
ItemStyle-BackColor="Gray">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="FilePath"
Visible="true"
ItemStyle-Width="310px"
HeaderStyle-Width="355px"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
AllowFiltering="false"
ItemStyle-BackColor="Gray">
<ItemTemplate>
<telerik:RadAsyncUpload runat="server" ID="RadUpload1">
</telerik:RadAsyncUpload>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
you can hook into ItemDataBound event of RadGrid, then find the control , you can check this links. Telerik RadGrid - databound Events.
SomeGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
RadAsyncUpload objUpload = (RadAsyncUpload) e.Item.FindControl("RadUpload1");
if(opbjUpload !=null)
{
// do some thing with Upload Obj.
}
}
}
You would be disable RadAsyncUpload Control during DataBinding / DataBound Item events of RadGrid.
DataBinding / DataBound are events Occurs when the server control binds to a data source.
(Inherited from Control.)
When you bind a RadGrid Control. for ex.
protected void Page_Load(object sender,System.EventArgs e)
{
if(!IsPostBack)
{
// Here I creates temporary datatable..
// you can generate dynamic DataTable from SQL query to fill DataSet/DataTable.
// Here I created temp DataTable for Binding RadGrid grid control..
DataTable dt = new DataTable("temp");
RadGrid.DataSource= dt;
RadGrid.DataBind();
// It will event fired When you binding data source.
// If You have to added "RadGrid_ItemDataBound" Item bound event to the <RADGRID >... control.
}
}
protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
RadAsyncUpload asyncUpload = (RadAsyncUpload) e.Item.FindControl("RadAsyncUploadControlID");
bool blUploadControlHide=true;
if(asyncUpload !=null)
{
if(blUploadControlHide)
{
asyncUpload.Enabled = false;
//If you can hide then write asyncUpload.Visible = false;
}
else
{
asyncUpload.Enabled = true;
}
}
}
}
Refer Radgrid Events
Thanks

Resources