473,225 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Trying to use FindControl

I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 22 '06 #1
11 1862
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

Dec 23 '06 #2
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #3
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.
>
string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;
>
-----------------------DataGrid-------------------------------------------
>
<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
>
>
Dec 23 '06 #4
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}
"Manish Bafna" wrote:
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}
>
Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #5
Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox

Thanks and Regards,
Manish Bafna.
MCP and MCTS.
"Mike Collins" wrote:
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}
"Manish Bafna" wrote:
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?
>
"Manish Bafna" wrote:
>
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:

I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.
>
string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;
>
-----------------------DataGrid-------------------------------------------
>
<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
>
>
Dec 23 '06 #6
I had to change your code where you had the "as TextBox" because it caused an
error to the following:

this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");

Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.

I appreciate your patience and help in trying to help me figure this out.

"Manish Bafna" wrote:
Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox

Thanks and Regards,
Manish Bafna.
MCP and MCTS.
"Mike Collins" wrote:
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}
"Manish Bafna" wrote:
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:

Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}
>
Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #7
Hi,
so finally it was solved or you r still getting some errors?

"Mike Collins" wrote:
I had to change your code where you had the "as TextBox" because it caused an
error to the following:

this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");

Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.

I appreciate your patience and help in trying to help me figure this out.

"Manish Bafna" wrote:
Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox

Thanks and Regards,
Manish Bafna.
MCP and MCTS.
"Mike Collins" wrote:
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."
>
private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}
>
>
"Manish Bafna" wrote:
>
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:

Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?
>
"Manish Bafna" wrote:
>
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:

I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.
>
string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;
>
-----------------------DataGrid-------------------------------------------
>
<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
>
>
Dec 23 '06 #8
Still an error.

error: identifier 'option' out of scope, when I checked the object "option"
after running the code. It was not a run time error, I did a quick watch and
saw it.

"Manish Bafna" wrote:
Hi,
so finally it was solved or you r still getting some errors?

"Mike Collins" wrote:
I had to change your code where you had the "as TextBox" because it caused an
error to the following:

this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");

Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.

I appreciate your patience and help in trying to help me figure this out.

"Manish Bafna" wrote:
Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
>
"Mike Collins" wrote:
>
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}


"Manish Bafna" wrote:

Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:

Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}
>
Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #9
Hi,
I think the error u r getting in quick watch is the bug in the microsoft
product.See below link:
http://www.kbalertz.com/Feedback_814828.aspx
I think you r now able to get text of textbox in datagrid.
"Mike Collins" wrote:
Still an error.

error: identifier 'option' out of scope, when I checked the object "option"
after running the code. It was not a run time error, I did a quick watch and
saw it.

"Manish Bafna" wrote:
Hi,
so finally it was solved or you r still getting some errors?

"Mike Collins" wrote:
I had to change your code where you had the "as TextBox" because it caused an
error to the following:
>
this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");
>
Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.
>
I appreciate your patience and help in trying to help me figure this out.
>
"Manish Bafna" wrote:
>
Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox

Thanks and Regards,
Manish Bafna.
MCP and MCTS.


"Mike Collins" wrote:

Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."
>
private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}
>
>
"Manish Bafna" wrote:
>
Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:

Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?
>
"Manish Bafna" wrote:
>
Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}

Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Mike Collins" wrote:

I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.
>
string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;
>
-----------------------DataGrid-------------------------------------------
>
<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
>
>
Dec 23 '06 #10
Ok...I'll check it again.

"Manish Bafna" wrote:
Hi,
I think the error u r getting in quick watch is the bug in the microsoft
product.See below link:
http://www.kbalertz.com/Feedback_814828.aspx
I think you r now able to get text of textbox in datagrid.
"Mike Collins" wrote:
Still an error.

error: identifier 'option' out of scope, when I checked the object "option"
after running the code. It was not a run time error, I did a quick watch and
saw it.

"Manish Bafna" wrote:
Hi,
so finally it was solved or you r still getting some errors?
>
"Mike Collins" wrote:
>
I had to change your code where you had the "as TextBox" because it caused an
error to the following:

this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");

Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.

I appreciate your patience and help in trying to help me figure this out.

"Manish Bafna" wrote:

Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
>
"Mike Collins" wrote:
>
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}


"Manish Bafna" wrote:

Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:

Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}
>
Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #11
Thank you for your help. Found out that I had a type in the html of the
datagrid that was causing the problem.

"Manish Bafna" wrote:
Hi,
I think the error u r getting in quick watch is the bug in the microsoft
product.See below link:
http://www.kbalertz.com/Feedback_814828.aspx
I think you r now able to get text of textbox in datagrid.
"Mike Collins" wrote:
Still an error.

error: identifier 'option' out of scope, when I checked the object "option"
after running the code. It was not a run time error, I did a quick watch and
saw it.

"Manish Bafna" wrote:
Hi,
so finally it was solved or you r still getting some errors?
>
"Mike Collins" wrote:
>
I had to change your code where you had the "as TextBox" because it caused an
error to the following:

this.dgDropDownMenus.EditItemIndex = e.Item.ItemIndex;
TextBox option =
(TextBox)dgDropDownMenus.Items[this.dgDropDownMenus.EditItemIndex].FindControl("txtName");

Then I got the following error: error: identifier 'option' out of scope,
when I checked the object "option" after running the code. It was not a run
time error, I did a quick watch.

I appreciate your patience and help in trying to help me figure this out.

"Manish Bafna" wrote:

Hi,
Try this one:
this.dgMenus.EditItemIndex = e.Item.ItemIndex;
dgMenus.Items[this.DataGrid1.EditItemIndex].FindControl("txtName") as TextBox
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
>
"Mike Collins" wrote:
>
Sorry for the incomplete response. Below is the event that is running, and I
am getting an error on the first line of the method "string option..."

private void dgDropDownMenus_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string option = ((TextBox)e.Item.FindControl("txtName")).Text;
Server.Transfer("../surveys/manageansweroptions.aspx?AnswerGroupID=" +
dgDropDownMenus.DataKeys[e.Item.ItemIndex].ToString() + "&CallingPage=Manage
Dropdowns Page&ReturnURL=ManageDropDowns.aspx&option=" +
rblOption.SelectedItem.Value + "&GroupName=" +
dgDropDownMenus.Items[e.Item.ItemIndex].Cells[2].Text);
}


"Manish Bafna" wrote:

Hi,
can you please tell at which line you are getting this error?Most probably
you would be using this line of findcontrol in update event of DataGrid.If
you give details as to where r using this code of findcontrol or in which
event u r using then only i would be able to help you out.
>
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
Thanks, but I get an error: Object reference not set to an instance of an
object. Any idea on why that is happening?

"Manish Bafna" wrote:

Hi,
It should be:
DataGrid dgDropDownMenus= (DataGrid)sender;
if(e.Item.ItemType.ToString() != "Header" && e.Item.ItemType.ToString() !=
"Footer")
{
string option = ((TextBox )e.Item.FindControl("txtName")).Text;
}
>
Although i have written this code for Datagrid, you can use same for
gridview with little or no modification.
Thanks and Regards,
Manish Bafna.
MCP and MCTS.
>
"Mike Collins" wrote:
>
I am trying to get the text of an item in a GridView, but am doing something
wrong. Can someone help me with the correct C# statement I need? Below is my
GridView and my attempt to get the control. Thank you.

string option =
((TextBox)dgDropDownMenus.Items[e.Item.ItemIndex].FindControl("txtName")).Text;

-----------------------DataGrid-------------------------------------------

<asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px"
runat="server" DataKeyField="DropDownMenuID" CellPadding="0"
BackColor="White" BorderStyle="None" Font-Bold="True" BorderWidth="1px"
BorderColor="#999999" GridLines="Vertical" HorizontalAlign="Center"
AutoGenerateColumns="False" Width="100%">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center"
Height="10px" ForeColor="White" VerticalAlign="Middle"
BackColor="#008A8C"></SelectedItemStyle>
<EditItemStyle HorizontalAlign="Center" Height="10px" Width="50px"
VerticalAlign="Middle"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" Height="10px"
VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" Height="10px" ForeColor="Black"
VerticalAlign="Middle"
BackColor="White"></ItemStyle>
<HeaderStyle Wrap="False" CssClass="datagridheader"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="<img border="0" src="../images/edit_icon.gif">"
HeaderText="Edit" CommandName="Edit">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="25px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtName" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DisplayName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Width="70%"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Description") %>' ID="Label2">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Textbox1" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Description") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Middle" NextPageText="Next -->"
PrevPageText="<-- Previous"
HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Wrap="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Dec 23 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
2
by: Neo Geshel | last post by:
After pouring over about a dozen sites that clearly dealt with ClientID all by itself, I came to the realization that about 2/3+ of them were doing it wrong. It is indeed impossible to grab the...
2
by: christof | last post by:
How to do it: My page: <asp:DataList ID="dataListRoleMembers" ...> .... <FooterTemplate> <asp:LinkButton ID="btnAddMember" runat="server"...
2
by: encoad | last post by:
Hi everyone, I'm slowly going mad with Masterpages and the FindControl. After a couple days of figuring out how to use the FindControl command from within a Masterpage, I still can't explain...
2
by: Henry Stock | last post by:
I don't seem to understand how to use the value: Request.ServerVariables("remote_addr") I am trying to pass the ip address of a sending web client in the body of an email message. When I...
7
by: AAaron123 | last post by:
Me.FindControl("MissionScheduleID"), below returns null. Do you know what I'm doing wrong? Thanks ***In my .aspx file I have: asp:Content ID="Content3"...
4
by: Hillbilly | last post by:
Maybe this is or isn't some kind of bug but it sure is goofy and remains a mystery that really has me puzzled for two reasons... // goofy syntax functions as expected... Panel finalStepButton =...
9
by: AAaron123 | last post by:
I'm this far in determining the correct code to find a textbox I need to set. ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.