|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Referencing to LinkButton in Gridview TemplateFieldI have the following GridView with a LinkButton in a TemplateField, <asp:GridView ID="ServersGrid" runat="server"> <Columns> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:LinkButton ID="lbDetails" runat="server" onfocus="this.blur()" Text="Details" CssClass="cntltext" Width="70" OnCommand="LinkButtonClick" CommandName="Details" CommandArgument='<%#Eval("Name") %>' Visible="false" /> </ItemTemplate> </asp:TemplateField> </Columns> I would like to change the Visible state in my Page_load code behind depending on some conditions as below but cannot locate the context name lbDetails.Visible = true; I also tried to use Page.FindControl("lbDetails") but returns null as well. Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView and what's the correct way of referencing the control ? Thanks Peter
Show quote
Hide quote
"Peter" <peter***@msn.com> wrote in message news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl... I also have a Checkbox inside a TemplateField as below> Hi > > I have the following GridView with a LinkButton in a TemplateField, > > <asp:GridView ID="ServersGrid" runat="server"> > <Columns> > <asp:TemplateField HeaderText="Action"> > <ItemTemplate> > <asp:LinkButton ID="lbDetails" runat="server" onfocus="this.blur()" Text="Details" > CssClass="cntltext" Width="70" OnCommand="LinkButtonClick" CommandName="Details" > CommandArgument='<%#Eval("Name") %>' Visible="false" /> > </ItemTemplate> > </asp:TemplateField> > </Columns> > > I would like to change the Visible state in my Page_load code behind depending on some conditions as below but cannot locate the > context name > > lbDetails.Visible = true; > > I also tried to use Page.FindControl("lbDetails") but returns null as well. > > Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView and what's the correct way > of referencing the control ? > > Thanks > Peter > <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> </ItemTemplate> </asp:TemplateField> I would like to reference the button checked status with below but also cannot locate the context name protected void cbConfirm_Change(Object sender, EventArgs e) { if (cbConfirmCheck.Checked) { } else { } } Would someone please kindly help. Thanks Peter "Peter" <peter***@msn.com> wrote in message if (e.Checked)news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl... > if (cbConfirmCheck.Checked) "Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uOHdN7j%23JHA.5064@TK2MSFTNGP03.phx.gbl... Hi Mark> "Peter" <peter***@msn.com> wrote in message news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl... > >> if (cbConfirmCheck.Checked) > > > if (e.Checked) > > > -- > Mark Rae > ASP.NET MVP > http://www.markrae.net I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked' <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> with the following code behind protected void cbConfirm_Change(Object sender, EventArgs e) { if (e.Checked) { } else { } }
Show quote
Hide quote
"Peter" <peter***@msn.com> wrote in message news:uoTFY4k%23JHA.5780@TK2MSFTNGP03.phx.gbl... Hi Mark> > "Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uOHdN7j%23JHA.5064@TK2MSFTNGP03.phx.gbl... >> "Peter" <peter***@msn.com> wrote in message news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl... >> >>> if (cbConfirmCheck.Checked) >> >> >> if (e.Checked) >> >> >> -- >> Mark Rae >> ASP.NET MVP >> http://www.markrae.net > > Hi Mark > > I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked' > > <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> > > > with the following code behind > > protected void cbConfirm_Change(Object sender, EventArgs e) > { > if (e.Checked) > { > } > else > { > } > } > > What if I also need to reference the cbConfirmedCheck checkbox from the event "Delete_Click' on another control, what's the reference syntax ? <ItemTemplate> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click" Width="70" Text="Delete"></asp:Button> </ItemTemplate> Thanks for your help. Peter
Show quote
Hide quote
"Peter" <peter***@msn.com> wrote in message Hmm...news:%23y6Sa8k%23JHA.5040@TK2MSFTNGP04.phx.gbl... > I have tried the followings but still gett CS0117: 'System.EventArgs' > does not contain a definition for 'Checked' > > <asp:CheckBox ID="cbConfirmCheck" runat="server" > OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> > > with the following code behind > > protected void cbConfirm_Change(Object sender, EventArgs e) > { > if (e.Checked) Try this instead: if(((CheckBox)sender).Checked) > What if I also need to reference the cbConfirmedCheck checkbox from the Since the CheckBox and the Button are in the same Column (and, therefore, in > event "Delete_Click' on another control, what's the reference syntax ? > <ItemTemplate> > <asp:CheckBox ID="cbConfirmCheck" > runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> > <asp:Button ID="DeleteButton" > CssClass="cntltext" runat="server" OnCommand="Delete_Click" > Width="70" Text="Delete"></asp:Button> > </ItemTemplate> the same tablecell once the GridView has been rendered into an HTML table), you can do something like this: ((CheckBox)((Button)sender).Parent.Controls[0]).Checked
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uu9nHHl%23JHA.5780@TK2MSFTNGP03.phx.gbl... This works.....thanks Mark> "Peter" <peter***@msn.com> wrote in message news:%23y6Sa8k%23JHA.5040@TK2MSFTNGP04.phx.gbl... > >> I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked' >> >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> >> >> with the following code behind >> >> protected void cbConfirm_Change(Object sender, EventArgs e) >> { >> if (e.Checked) > > Hmm... > > Try this instead: > > if(((CheckBox)sender).Checked) > Show quoteHide quote >> What if I also need to reference the cbConfirmedCheck checkbox from the event "Delete_Click' on another control, what's the Hm....don't quite understand the correct syntax in the Delete_Click event....can I still reference it by "sender" ? When I tried, >> reference syntax ? > >> <ItemTemplate> >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> >> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click" >> Width="70" Text="Delete"></asp:Button> >> </ItemTemplate> > > Since the CheckBox and the Button are in the same Column (and, therefore, in the same tablecell once the GridView has been > rendered into an HTML table), you can do something like this: > > ((CheckBox)((Button)sender).Parent.Controls[0]).Checked > > I get the following.... Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.CheckBox'. Source Error: Line 108: protected void Delete_Click(Object sender, CommandEventArgs e) Line 109: { Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked)
Show quote
Hide quote
"Peter" <peter***@msn.com> wrote in message You said you wanted to reference the CheckBox from the Delete Button - if news:uWTtsUl%23JHA.4560@TK2MSFTNGP05.phx.gbl... > Hm....don't quite understand the correct syntax in the Delete_Click > event....can I still reference it by "sender" ? When I tried, I get the > following.... > > Exception Details: System.InvalidCastException: Unable to cast object of > type 'System.Web.UI.LiteralControl' to type > 'System.Web.UI.WebControls.CheckBox'. > > Source Error: > > Line 108: protected void Delete_Click(Object sender, CommandEventArgs > e) > Line 109: { > Line 110: if > (((CheckBox)((Button)sender).Parent.Controls[0]).Checked) the CheckBox isn't the first control in the Cell, you'll need to modify Controls[0] accordingly...
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:ecSWnZl%23JHA.1252@TK2MSFTNGP04.phx.gbl... The checkbox is the first control in the cell as below, below is the exact code that I have, any comments ?> "Peter" <peter***@msn.com> wrote in message news:uWTtsUl%23JHA.4560@TK2MSFTNGP05.phx.gbl... > >> Hm....don't quite understand the correct syntax in the Delete_Click event....can I still reference it by "sender" ? When I >> tried, I get the following.... >> >> Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type >> 'System.Web.UI.WebControls.CheckBox'. >> >> Source Error: >> >> Line 108: protected void Delete_Click(Object sender, CommandEventArgs e) >> Line 109: { >> Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked) > > > You said you wanted to reference the CheckBox from the Delete Button - if the CheckBox isn't the first control in the Cell, you'll > need to modify Controls[0] accordingly... > > <ItemTemplate> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click" Width="70" Text="Delete"></asp:Button> </ItemTemplate> Peter
Show quote
Hide quote
"Peter" <peter***@msn.com> wrote in message 1) Put a breakpoint on line 110 abovenews:uImScdl%23JHA.1376@TK2MSFTNGP02.phx.gbl... >>> Line 110: if >>> (((CheckBox)((Button)sender).Parent.Controls[0]).Checked) >> >> You said you wanted to reference the CheckBox from the Delete Button - if >> the CheckBox isn't the first control in the Cell, you'll need to modify >> Controls[0] accordingly... >> > The checkbox is the first control in the cell as below, below is the exact > code that I have, any comments ? > > <ItemTemplate> > <asp:CheckBox ID="cbConfirmCheck" > runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> > <asp:Button ID="DeleteButton" > CssClass="cntltext" runat="server" OnCommand="Delete_Click" > Width="70" Text="Delete"></asp:Button> > </ItemTemplate> 2) Inspect the value of sender - is it *definitely* the Delete button? 3) Assuming it is, inspect ((Button)sender.Parent.Controls.Count - how many Controls are in the Delete Button's Parent (i.e. Cell)? 4) Inspect the type of each control - GetType().Name Essentially, what you're trying to do is walk backwards through the control hierarchy until you find the Control you're interested in...
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23fOBzhl%23JHA.1380@TK2MSFTNGP02.phx.gbl... Hi Mark> "Peter" <peter***@msn.com> wrote in message news:uImScdl%23JHA.1376@TK2MSFTNGP02.phx.gbl... > >>>> Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked) >>> >>> You said you wanted to reference the CheckBox from the Delete Button - if the CheckBox isn't the first control in the Cell, >>> you'll need to modify Controls[0] accordingly... >>> >> The checkbox is the first control in the cell as below, below is the exact code that I have, any comments ? >> >> <ItemTemplate> >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox> >> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click" >> Width="70" Text="Delete"></asp:Button> >> </ItemTemplate> > > 1) Put a breakpoint on line 110 above > > 2) Inspect the value of sender - is it *definitely* the Delete button? > > 3) Assuming it is, inspect ((Button)sender.Parent.Controls.Count - how many Controls are in the Delete Button's Parent (i.e. > Cell)? > > 4) Inspect the type of each control - GetType().Name > > Essentially, what you're trying to do is walk backwards through the control hierarchy until you find the Control you're interested > in... > I managed to get the following syntax to get what I want. ((CheckBox)((Button)sender).Parent.Controls[0].FindControl("cbConfirmCheck")).Checked Thanks for your help again. Peter "Peter" <peter***@msn.com> wrote in message It can...news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl... > Can someone let me know why the name lbDetails cannot be located within a > TemplateField in a GridView > and what's the correct way of referencing the control ? In which of the GridView's methods are you trying to reference it?Please show some code...
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23Pi2Y5j%23JHA.2872@TK2MSFTNGP05.phx.gbl... Hi Mark> "Peter" <peter***@msn.com> wrote in message news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl... > >> Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView > > It can... > > >> and what's the correct way of referencing the control ? > > In which of the GridView's methods are you trying to reference it? > > Please show some code... > > > -- > Mark Rae > ASP.NET MVP > http://www.markrae.net I would like to reference it from Page_Load, is that possible ? Or do I need to reference it from the OnRowDataBound method ? Because whether the button is shown can be determined from the beginning of the page and it applies to ALL rows. Therefore I just need to apply it when the page is being loaded. Thanks Peter "Peter" <peter***@msn.com> wrote in message The thing here is that there is no "it"... Controls inside a GridView are news:OtyJf6k%23JHA.1376@TK2MSFTNGP02.phx.gbl... >>> and what's the correct way of referencing the control ? >> >> In which of the GridView's methods are you trying to reference it? >> >> Please show some code... > > I would like to reference it from Page_Load, is that possible? not really accessible from Page_Load because they don't really "exist" until the databinding process begins. When the page opens, it has no way of knowing how many of these controls will eventually be created in databound controls because it doesn't know how many rows there will be - maybe there won't be any at all... > Or do I need to reference it from the OnRowDataBound method ? Because If the button is in a separate column, you can simply hide that column > whether the button is shown can be determined from the beginning of the > page and it applies to ALL rows. Therefore I just need to apply it when > the page is being loaded. (.Visible = false) after the databinding process. If it is in the same column as other controls, you will need to hide each "instance" of it on a row-by-row basis in the RowDataBound method...
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23nwiaLl%23JHA.1336@TK2MSFTNGP05.phx.gbl... Thanks for your detail explanation. Is it the same applies to Page_LoadCompleted ?> "Peter" <peter***@msn.com> wrote in message news:OtyJf6k%23JHA.1376@TK2MSFTNGP02.phx.gbl... > >>>> and what's the correct way of referencing the control ? >>> >>> In which of the GridView's methods are you trying to reference it? >>> >>> Please show some code... >> >> I would like to reference it from Page_Load, is that possible? > > The thing here is that there is no "it"... Controls inside a GridView are not really accessible from Page_Load because they don't > really "exist" until the databinding process begins. When the page opens, it has no way of knowing how many of these controls will > eventually be created in databound controls because it doesn't know how many rows there will be - maybe there won't be any at > all... > > I will give it a try in RowDataBound method in this case... thanks again>> Or do I need to reference it from the OnRowDataBound method ? Because whether the button is shown can be determined from the >> beginning of the page and it applies to ALL rows. Therefore I just need to apply it when the page is being loaded. > > If the button is in a separate column, you can simply hide that column (.Visible = false) after the databinding process. If it is > in the same column as other controls, you will need to hide each "instance" of it on a row-by-row basis in the RowDataBound > method... > Peter
Other interesting topics
progress indicator long running proc NO Button click
Help with C# using popup pox Commented codes still affect the program Web Deployment Project - Publish? Displaying a generated picture in MS Word Image does not fit correctly in a div frame Multiple Select commands Localization issue with master page. Boundfield display value BUG? Repeating tag syntax in VS 2008 source mode...? |
|||||||||||||||||||||||