Saturday, March 31, 2012
set ASP.NET datagrid image based on SQL table value
one image if the value in my SQL table is true and another if it is
false. Here is my code.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3"
AutoGenerateColumns="False" CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<Columns>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<img src='\Check.gif'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>
I want to show the 'Check.gif' if an SQL table value is True or
'x.gif' if it is false.
Thaks for any help, This ASP.NET is kicking my butt.Hi Rob,
Just jam a little
<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'>
in there and you should be okay.
BTW, I had some problems with your markup. My fixed version is below.
Does this help?
Ken
Microsoft MVP [ASP.NET]
Toronto
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" runat="server" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3" AutoGenerateColumns="False"
CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>
"Rob Rogers" <rob@.natltc.com> wrote in message
news:7ee34208.0407141234.18d2abdd@.posting.google.com...
>I have an ASP.NET datagrid that loads an image. I would like to load
> one image if the value in my SQL table is true and another if it is
> false. Here is my code.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <title>WebForm1</title>
> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> <meta name="vs_defaultClientScript" content="VBScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
> absolute; TOP: 0px" runat="server"
> BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
> BackColor="#DEBA84" CellPadding="3"
> AutoGenerateColumns="False" CellSpacing="2">
> <SelectedItemStyle Font-Bold="True" ForeColor="White"
> BackColor="#738A9C"></SelectedItemStyle>
> <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="White"
> BackColor="#A55129"></HeaderStyle>
> <FooterStyle ForeColor="#8C4510"
> BackColor="#F7DFB5"></FooterStyle>
> <Columns>
> </asp:BoundColumn>
> <asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
> <ItemStyle Width="150px"></ItemStyle>
> </asp:BoundColumn>
> <asp:TemplateColumn HeaderText="Check or X">
> <ItemTemplate>
> <img src='\Check.gif'>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> </Columns>
> <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
> Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> </form>
> </body>
> </HTML>
> I want to show the 'Check.gif' if an SQL table value is True or
> 'x.gif' if it is false.
> Thaks for any help, This ASP.NET is kicking my butt.
BTW, here's the code behind and data source that I used for that example.
"Boolean" is the name of the field as well as the type.
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
MyGrid.DataSource = CreateDataSource()
MyGrid.DataBind()
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("City", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
"Rob Rogers" <rob@.natltc.com> wrote in message
news:7ee34208.0407141234.18d2abdd@.posting.google.com...
>I have an ASP.NET datagrid that loads an image. I would like to load
> one image if the value in my SQL table is true and another if it is
> false. Here is my code.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <title>WebForm1</title>
> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> <meta name="vs_defaultClientScript" content="VBScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
> absolute; TOP: 0px" runat="server"
> BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
> BackColor="#DEBA84" CellPadding="3"
> AutoGenerateColumns="False" CellSpacing="2">
> <SelectedItemStyle Font-Bold="True" ForeColor="White"
> BackColor="#738A9C"></SelectedItemStyle>
> <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="White"
> BackColor="#A55129"></HeaderStyle>
> <FooterStyle ForeColor="#8C4510"
> BackColor="#F7DFB5"></FooterStyle>
> <Columns>
> </asp:BoundColumn>
> <asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
> <ItemStyle Width="150px"></ItemStyle>
> </asp:BoundColumn>
> <asp:TemplateColumn HeaderText="Check or X">
> <ItemTemplate>
> <img src='\Check.gif'>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> </Columns>
> <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
> Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> </form>
> </body>
> </HTML>
> I want to show the 'Check.gif' if an SQL table value is True or
> 'x.gif' if it is false.
> Thaks for any help, This ASP.NET is kicking my butt.
Thanks, Thats just what I needed.
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!
set ASP.NET datagrid image based on SQL table value
one image if the value in my SQL table is true and another if it is
false. Here is my code.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3"
AutoGenerateColumns="False" CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<Columns>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<img src='\Check.gif'>
</ItemTemplate>
</asp:TemplateColumn
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML
I want to show the 'Check.gif' if an SQL table value is True or
'x.gif' if it is false.
Thaks for any help, This ASP.NET is kicking my butt.Hi Rob,
Just jam a little
<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'
in there and you should be okay.
BTW, I had some problems with your markup. My fixed version is below.
Does this help?
Ken
Microsoft MVP [ASP.NET]
Toronto
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" runat="server" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3" AutoGenerateColumns="False"
CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML
"Rob Rogers" <rob@.natltc.com> wrote in message
news:7ee34208.0407141234.18d2abdd@.posting.google.c om...
>I have an ASP.NET datagrid that loads an image. I would like to load
> one image if the value in my SQL table is true and another if it is
> false. Here is my code.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <title>WebForm1</title>
> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> <meta name="vs_defaultClientScript" content="VBScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
> absolute; TOP: 0px" runat="server"
> BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
> BackColor="#DEBA84" CellPadding="3"
> AutoGenerateColumns="False" CellSpacing="2">
> <SelectedItemStyle Font-Bold="True" ForeColor="White"
> BackColor="#738A9C"></SelectedItemStyle>
> <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="White"
> BackColor="#A55129"></HeaderStyle>
> <FooterStyle ForeColor="#8C4510"
> BackColor="#F7DFB5"></FooterStyle>
> <Columns>
> </asp:BoundColumn>
> <asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
> <ItemStyle Width="150px"></ItemStyle>
> </asp:BoundColumn>
> <asp:TemplateColumn HeaderText="Check or X">
> <ItemTemplate>
> <img src='\Check.gif'>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> </Columns>
> <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
> Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> </form>
> </body>
> </HTML>
> I want to show the 'Check.gif' if an SQL table value is True or
> 'x.gif' if it is false.
> Thaks for any help, This ASP.NET is kicking my butt.
BTW, here's the code behind and data source that I used for that example.
"Boolean" is the name of the field as well as the type.
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
MyGrid.DataSource = CreateDataSource()
MyGrid.DataBind()
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("City", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
"Rob Rogers" <rob@.natltc.com> wrote in message
news:7ee34208.0407141234.18d2abdd@.posting.google.c om...
>I have an ASP.NET datagrid that loads an image. I would like to load
> one image if the value in my SQL table is true and another if it is
> false. Here is my code.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <title>WebForm1</title>
> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
> <meta name="vs_defaultClientScript" content="VBScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form id="Form1" method="post" runat="server">
> <asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
> absolute; TOP: 0px" runat="server"
> BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
> BackColor="#DEBA84" CellPadding="3"
> AutoGenerateColumns="False" CellSpacing="2">
> <SelectedItemStyle Font-Bold="True" ForeColor="White"
> BackColor="#738A9C"></SelectedItemStyle>
> <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
> <HeaderStyle Font-Bold="True" ForeColor="White"
> BackColor="#A55129"></HeaderStyle>
> <FooterStyle ForeColor="#8C4510"
> BackColor="#F7DFB5"></FooterStyle>
> <Columns>
> </asp:BoundColumn>
> <asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
> <ItemStyle Width="150px"></ItemStyle>
> </asp:BoundColumn>
> <asp:TemplateColumn HeaderText="Check or X">
> <ItemTemplate>
> <img src='\Check.gif'>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> </Columns>
> <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
> Mode="NumericPages"></PagerStyle>
> </asp:DataGrid>
> </form>
> </body>
> </HTML>
> I want to show the 'Check.gif' if an SQL table value is True or
> 'x.gif' if it is false.
> Thaks for any help, This ASP.NET is kicking my butt.
Thanks, Thats just what I needed.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Set border width for datagrid
I have a dropdown and want the user to toggle border width or alternating
color etc for datagrid.
How can i implement this in codebehind?
Thanks
MannyUse the property AlternatingItemStyle
Cheers,
Gaurav Vaish
http://mastergaurav.org
--
Thursday, March 29, 2012
set cell color while rendered in a datagrid
SortExpression="EFF_END_DT" HeaderText="Effective End"
DataFormatString="{0:d}">
<ItemStyle ForeColor= '<% # MyColor(
DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%>'></ItemStyle>
</asp:BoundColumn>
to have red date is it is over and blue if not!
But I get an error:
Compiler Error Message: BC30676: 'DataBinding' is not an event of
'System.Web.UI.WebControls.TableItemStyle'.
how can I resolve this?i think your trying to user the OnItemDataBound method
In this method you can access each row of your datagrid, and from there
determine if the date should be read or not.
in the .aspx page
<asp:DataGrid id="MyGrid" OnItemDataBound="MyGrid_OnItemDataBound" ....
then in the .cs page
public void MyGrid_OnItemDataBound(object Sender, DataGridItemEventArgs e)
{
//From here you can access your bound column and set the colors
}
"Ofer" <Ofer@.discussions.microsoft.com> wrote in message
news:574957A7-2D07-44A7-95BD-69B36E77C458@.microsoft.com...
>I use the code: <asp:BoundColumn DataField="EFF_END_DT"
> SortExpression="EFF_END_DT" HeaderText="Effective End"
> DataFormatString="{0:d}">
> <ItemStyle ForeColor= '<% # MyColor(
> DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%>'></ItemStyle>
> </asp:BoundColumn>
> to have red date is it is over and blue if not!
> But I get an error:
> Compiler Error Message: BC30676: 'DataBinding' is not an event of
> 'System.Web.UI.WebControls.TableItemStyle'.
> how can I resolve this?
>
Thanks! that was helpful.
Would you know why if the results is 2 rows the event fires 4 times
2 times with the value I axpect an 2 with " "
my vb is:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Debug.WriteLine(e.Item.Cells(6).GetType.ToString) 'always
System.Web.UI.WebControls.TableCell
Debug.WriteLine(e.Item.Cells(6).Text)
End Sub
End Class
"Grant Merwitz" wrote:
> i think your trying to user the OnItemDataBound method
> In this method you can access each row of your datagrid, and from there
> determine if the date should be read or not.
> in the .aspx page
> <asp:DataGrid id="MyGrid" OnItemDataBound="MyGrid_OnItemDataBound" ....
> then in the .cs page
> public void MyGrid_OnItemDataBound(object Sender, DataGridItemEventArgs e)
> {
> //From here you can access your bound column and set the colors
> }
>
> "Ofer" <Ofer@.discussions.microsoft.com> wrote in message
> news:574957A7-2D07-44A7-95BD-69B36E77C458@.microsoft.com...
>
>
You can use
If e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType = ListItemType.AlternatingItem Then
' Your code here
End If
HTH
Elton Wang
elton_wang@.hotmail.com
>--Original Message--
>Thanks! that was helpful.
>Would you know why if the results is 2 rows the event
fires 4 times
>2 times with the value I axpect an 2 with " "
>my vb is:
> Private Sub DataGrid1_ItemDataBound(ByVal sender As
Object, ByVal e As
>System.Web.UI.WebControls.DataGridItemEventArgs) Handles
>DataGrid1.ItemDataBound
> Debug.WriteLine(e.Item.Cells
(6).GetType.ToString) 'always
>System.Web.UI.WebControls.TableCell
> Debug.WriteLine(e.Item.Cells(6).Text)
> End Sub
>End Class
>"Grant Merwitz" wrote:
>
datagrid, and from there
OnItemDataBound="MyGrid_OnItemDataBound" ....
DataGridItemEventArgs e)
set the colors
message
69B36E77C458@.microsoft.com...
>'></ItemStyle>
an event of
>.
>
set cell color while rendered in a datagrid
SortExpression="EFF_END_DT" HeaderText="Effective End"
DataFormatString="{0:d}">
<ItemStyle ForeColor= '<% # MyColor(
DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%>'></ItemStyle>
</asp:BoundColumn>
to have red date is it is over and blue if not!
But I get an error:
Compiler Error Message: BC30676: 'DataBinding' is not an event of
'System.Web.UI.WebControls.TableItemStyle'.
how can I resolve this?i think your trying to user the OnItemDataBound method
In this method you can access each row of your datagrid, and from there
determine if the date should be read or not.
in the .aspx page
<asp:DataGrid id="MyGrid" OnItemDataBound="MyGrid_OnItemDataBound" ....
then in the .cs page
public void MyGrid_OnItemDataBound(object Sender, DataGridItemEventArgs e)
{
//From here you can access your bound column and set the colors
}
"Ofer" <Ofer@.discussions.microsoft.com> wrote in message
news:574957A7-2D07-44A7-95BD-69B36E77C458@.microsoft.com...
>I use the code: <asp:BoundColumn DataField="EFF_END_DT"
> SortExpression="EFF_END_DT" HeaderText="Effective End"
> DataFormatString="{0:d}">
> <ItemStyle ForeColor= '<% # MyColor(
> DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%>'></ItemStyle>
> </asp:BoundColumn>
> to have red date is it is over and blue if not!
> But I get an error:
> Compiler Error Message: BC30676: 'DataBinding' is not an event of
> 'System.Web.UI.WebControls.TableItemStyle'.
> how can I resolve this?
Thanks! that was helpful.
Would you know why if the results is 2 rows the event fires 4 times
2 times with the value I axpect an 2 with " "
my vb is:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Debug.WriteLine(e.Item.Cells(6).GetType.ToString) 'always
System.Web.UI.WebControls.TableCell
Debug.WriteLine(e.Item.Cells(6).Text)
End Sub
End Class
"Grant Merwitz" wrote:
> i think your trying to user the OnItemDataBound method
> In this method you can access each row of your datagrid, and from there
> determine if the date should be read or not.
> in the .aspx page
> <asp:DataGrid id="MyGrid" OnItemDataBound="MyGrid_OnItemDataBound" ....
> then in the .cs page
> public void MyGrid_OnItemDataBound(object Sender, DataGridItemEventArgs e)
> {
> //From here you can access your bound column and set the colors
> }
>
> "Ofer" <Ofer@.discussions.microsoft.com> wrote in message
> news:574957A7-2D07-44A7-95BD-69B36E77C458@.microsoft.com...
> >I use the code: <asp:BoundColumn DataField="EFF_END_DT"
> > SortExpression="EFF_END_DT" HeaderText="Effective End"
> > DataFormatString="{0:d}">
> > <ItemStyle ForeColor= '<% # MyColor(
> > DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%>'></ItemStyle>
> > </asp:BoundColumn>
> > to have red date is it is over and blue if not!
> > But I get an error:
> > Compiler Error Message: BC30676: 'DataBinding' is not an event of
> > 'System.Web.UI.WebControls.TableItemStyle'.
> > how can I resolve this?
>
You can use
If e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType = ListItemType.AlternatingItem Then
' Your code here
End If
HTH
Elton Wang
elton_wang@.hotmail.com
>--Original Message--
>Thanks! that was helpful.
>Would you know why if the results is 2 rows the event
fires 4 times
>2 times with the value I axpect an 2 with " "
>my vb is:
> Private Sub DataGrid1_ItemDataBound(ByVal sender As
Object, ByVal e As
>System.Web.UI.WebControls.DataGridItemEventArgs) Handles
>DataGrid1.ItemDataBound
> Debug.WriteLine(e.Item.Cells
(6).GetType.ToString) 'always
>System.Web.UI.WebControls.TableCell
> Debug.WriteLine(e.Item.Cells(6).Text)
> End Sub
>End Class
>"Grant Merwitz" wrote:
>> i think your trying to user the OnItemDataBound method
>>
>> In this method you can access each row of your
datagrid, and from there
>> determine if the date should be read or not.
>>
>> in the .aspx page
>> <asp:DataGrid id="MyGrid"
OnItemDataBound="MyGrid_OnItemDataBound" ....
>>
>> then in the .cs page
>>
>> public void MyGrid_OnItemDataBound(object Sender,
DataGridItemEventArgs e)
>> {
>> //From here you can access your bound column and
set the colors
>> }
>>
>>
>> "Ofer" <Ofer@.discussions.microsoft.com> wrote in
message
>> news:574957A7-2D07-44A7-95BD-
69B36E77C458@.microsoft.com...
>> >I use the code: <asp:BoundColumn DataField="EFF_END_DT"
>> > SortExpression="EFF_END_DT" HeaderText="Effective End"
>> > DataFormatString="{0:d}">
>> > <ItemStyle ForeColor= '<% # MyColor(
>> > DataBinder.Eval(Container.DataItem, "EFF_END_DT"))%
>'></ItemStyle>
>> > </asp:BoundColumn>
>> > to have red date is it is over and blue if not!
>> > But I get an error:
>> > Compiler Error Message: BC30676: 'DataBinding' is not
an event of
>> > 'System.Web.UI.WebControls.TableItemStyle'.
>>> > how can I resolve this?
>>>>
>>
>>
>.
Set checkbox in datagrid manually/through code
false. I am trying to populate a checkbox in a datagrid. In my SQL
statement, I used the Decode function so that when it populates the
dataset that I would store the string 'true' and 'false' rather than a
numeric value. I did this because I thought it would make it easier
to actually set the value in the checkbox. Anyway, I bind the dataset
to the datagrid. In my HTML code, the checkbox is contained in a
templatecolumn:
<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%#
(bool) DataBinder.Eval(Container.DataItem,
"strainer_on_fuel_line_bool") %>' Enabled="False"></asp:CheckBox
However, this will not work. It complains that "Specified cast is not
valid."
I have even tried it without the "(bool)" cast and it still didn't
work.
The data in my database has the value stored as either a -1 or 0. In
either case, I just want to set the check box to checked or unchecked.
I was even wondering if I needed to use one of the d atagrid's events
to do this.
Any ideas or help is appreciated.
Thanks,
MartySome suggestions
Try using a helper function to interpret your -1 or 0 instead of adjusting your recordset to return true or false
In your codebehind
protected bool GetCheckBoxSetting(object myObj
int myInt = Convert.ToInt16(myObj)
return (myInt == -1)
Then in your aspx
<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%
GetCheckBoxSetting(DataBinder.Eval(Container.DataI tem, "strainer_on_fuel_line_bool")) %>' Enabled="False"></asp:CheckBox
If returning bool doesn't set the checkbox checked property correctly then try returning a string from GetCheckBoxSetting method
protected string GetCheckBoxSetting(object myObj
int myInt = Convert.ToInt16(myObj)
if (myInt == -1
return "true"
els
return "false"
HTH
Suresh
-- Marty wrote: --
I have a numeric Oracle field which stores -1 for true and 0 fo
false. I am trying to populate a checkbox in a datagrid. In my SQ
statement, I used the Decode function so that when it populates th
dataset that I would store the string 'true' and 'false' rather than
numeric value. I did this because I thought it would make it easie
to actually set the value in the checkbox. Anyway, I bind the datase
to the datagrid. In my HTML code, the checkbox is contained in
templatecolumn
<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%
(bool) DataBinder.Eval(Container.DataItem
"strainer_on_fuel_line_bool") %>' Enabled="False"></asp:CheckBox
However, this will not work. It complains that "Specified cast is no
valid.
I have even tried it without the "(bool)" cast and it still didn'
work
The data in my database has the value stored as either a -1 or 0. I
either case, I just want to set the check box to checked or unchecked
I was even wondering if I needed to use one of the d atagrid's event
to do this
Any ideas or help is appreciated
Thanks,
Mart
It worked, thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Set Column Headertext
I have a Datagrid with a templatecolumn, where I add at design time. I also checked the "create columns automatically"
I would like to change the headertext of columns created automatically, but i don't know in which event I can do that
Can you help me
P.S. I tryied to set the headertext in the "PreRender" event of the DataGrid, but in this point the grid has still only the templatecolumn, and the others columns aren't created yet
Thank you
Alessandro RossHi, Alessandro Rossi,
If you extend the DataGrid class (inherit from it) the best place is the
OnDataBinding protected method. You should first call base.OnDataBinding(e);
of course.
If you are simply using the DataGrid class - set the headertexts after you
call DataBind() because the columns are in fact created with this call.
Hope this helps
Martin
"Alessandro Rossi" <alessandro.rossi@.areait.net> wrote in message
news:528CD483-7F36-4D2A-8760-24CD36F0BE8A@.microsoft.com...
> Hi,
> I have a Datagrid with a templatecolumn, where I add at design time. I
also checked the "create columns automatically".
> I would like to change the headertext of columns created automatically,
but i don't know in which event I can do that.
> Can you help me?
> P.S. I tryied to set the headertext in the "PreRender" event of the
DataGrid, but in this point the grid has still only the templatecolumn, and
the others columns aren't created yet.
> Thank you.
> Alessandro Rossi
Set columns in dataGrid
//Perform search
xslt.Transform(new XPathDocument(Server.MapPath("TIPTREECAT.xml")), xslArg,
sw, null);
StringReader sreader = new System.IO.StringReader("<root>" + sw.ToString() +
"</root>");
this.dataSet.ReadXml(sreader);
this.DataGrid.DataSource = dataSet;
this.DataGrid.DataBind();
How can I change colums I want to show in the grids? I want to show less
columns then there are in an xml data.
Thanksset the AutoGenerateColumns property to False. Then create column templates
for each column you want to display. The simplest version if this is the
BoundColumn:
<asp:BoundColumn DataField="FName"></asp:BoundColumn
You can also right-mouse click the datagrid and go to Property Builder.
This utility will help you set up your datagrid.
"Mark Goldin" <markgoldin@.comcast.net> wrote in message
news:eV#EXH0#DHA.3668@.TK2MSFTNGP09.phx.gbl...
> I have this code:
> //Perform search
> xslt.Transform(new XPathDocument(Server.MapPath("TIPTREECAT.xml")),
xslArg,
> sw, null);
> StringReader sreader = new System.IO.StringReader("<root>" + sw.ToString()
+
> "</root>");
> this.dataSet.ReadXml(sreader);
> this.DataGrid.DataSource = dataSet;
> this.DataGrid.DataBind();
>
> How can I change colums I want to show in the grids? I want to show less
> columns then there are in an xml data.
> Thanks
What is "BoundColumn"?
"Bob Boran" <mcsdsmurf@.hotmail.com> wrote in message
news:eEkdDc0#DHA.3220@.TK2MSFTNGP10.phx.gbl...
> set the AutoGenerateColumns property to False. Then create column
templates
> for each column you want to display. The simplest version if this is the
> BoundColumn:
> <asp:BoundColumn DataField="FName"></asp:BoundColumn>
> You can also right-mouse click the datagrid and go to Property Builder.
> This utility will help you set up your datagrid.
>
> "Mark Goldin" <markgoldin@.comcast.net> wrote in message
> news:eV#EXH0#DHA.3668@.TK2MSFTNGP09.phx.gbl...
> > I have this code:
> > //Perform search
> > xslt.Transform(new XPathDocument(Server.MapPath("TIPTREECAT.xml")),
> xslArg,
> > sw, null);
> > StringReader sreader = new System.IO.StringReader("<root>" +
sw.ToString()
> +
> > "</root>");
> > this.dataSet.ReadXml(sreader);
> > this.DataGrid.DataSource = dataSet;
> > this.DataGrid.DataBind();
> > How can I change colums I want to show in the grids? I want to show less
> > columns then there are in an xml data.
> > Thanks
Check the following link for sample and explanation of a boundcolumn. (watch for line wrap
http://samples.gotdotnet.com/quicks...rid.aspx#column
HTH
Suresh
-- Mark Goldin wrote: --
What is "BoundColumn"
"Bob Boran" <mcsdsmurf@.hotmail.com> wrote in messag
news:eEkdDc0#DHA.3220@.TK2MSFTNGP10.phx.gbl..
> set the AutoGenerateColumns property to False. Then create colum
template
> for each column you want to display. The simplest version if this is th
> BoundColumn
>><asp:BoundColumn DataField="FName"></asp:BoundColumn>>> You can also right-mouse click the datagrid and go to Property Builder
> This utility will help you set up your datagrid
>>> "Mark Goldin" <markgoldin@.comcast.net> wrote in messag
> news:eV#EXH0#DHA.3668@.TK2MSFTNGP09.phx.gbl..
>> I have this code
>> //Perform searc
>>>> xslt.Transform(new XPathDocument(Server.MapPath("TIPTREECAT.xml"))
> xslArg
>> sw, null)
>>>> StringReader sreader = new System.IO.StringReader("<root>"
sw.ToString(
>> "</root>")
>>>> this.dataSet.ReadXml(sreader)
>>>> this.DataGrid.DataSource = dataSet
>>>> this.DataGrid.DataBind()
>>>>>>>> How can I change colums I want to show in the grids? I want to show les
>> columns then there are in an xml data
>>>> Thank
>>>>>>
Set datagrid cell enabled to false
Here is my code that I tried:Dim cn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;" _
& "Persist Security Info=False;" _
& "Initial Catalog=fuel info;" _
& "UID=;PWD=;" _
& "Data Source=Server7")
cn.Open()
Dim datAdapt As New OleDb.OleDbDataAdapter("Select Vendor_No as [Vendor No], " & _
"Vendor_Name as [Vendor Name], Vendor_Terminal as [Vendor Terminal], " & _
"Vendor_Trmnl_No As [Terminal No], Vendor_City As [City], Vendor_State As " & _
"[State], ID From Other_Vendors Order By Vendor_Name", cn)
Dim datSet As New Data.DataSet
datAdapt.Fill(datSet, "Other_Vendors")
DataGrid1.DataSource = datSet
DataGrid1.DataKeyField = "ID"
DataGrid1.EditItemIndex = e.Item.ItemIndex
e.Item.Cells(3).Enabled = False
DataGrid1.DataBind()
cn.Close()
cn.Dispose()
datAdapt.Dispose()
datSet.Dispose()
As you can see, the line that is not working is "e.item.cells(3).enabled = false"
Also, if anyone knows how to set the width of the cell on the fly as well, I would appreciate that as well.
ThanksTry looking into the ItemDataBound sub and adding
e.Item.Cells(3).Enabled = False
in there. You cant disable the cells before the data is in them. RIght now you are trying to disable that cell before the data has been bound to the grid.
Jerel
Phelnglai, that did the trick. One more quick question. Could you set the cell width from the same place? If so, do you know the syntax? Thanks.
What about the cell header? What would the syntax be for that?
Thanks again!
Nevermind, I see that if you just increase the width of the actual datagrid, the fields will increase and adjust on their own at run time.
Glad you got it. Sorry I never answered your second question. I was in a car wreck and am just now getting back to work and doing thinks.
If you have any other questions, feel free to ask.
Jerel
Monday, March 26, 2012
set dropdownlist selectedindex in datagrid
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
I have a templatecolumn as follows:
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
with an edit column as:
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel" EditText="Edit"></
asp:EditCommandColumn>
I would like to set the dropdownlist to the value found in the
database. my code behind uses ItemDataBound as follows:
public void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dd;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
}
}
upon loading the dropdownlist has the values as in the database.
However, when I press edit the selected index becomes 0. I cannot
figure how to set the value correctly.
my edit code looks like:
public void myDataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dd;
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
<-- dd.SelectedIndex looks fine here -->
...
}
dd.SelectedIndex looks ok in debug mode, but something changes it
before the dropdownlist is displayed. the values in the dropdownlists
not being edited are ok, it's only the one being edited that shows the
wrong selectedindex.
any help would be appreciated. thanks.pleaseexplaintome@.yahoo.com wrote:
Quote:
Originally Posted by
Hi all,
>
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
>
I have a templatecolumn as follows:
>
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
There is a much easier way (at least in ASP.NET 2.0)
Set the property SelectedValue on the DrowDownlist
declaratively like this:
SelectedValue='<%# Bind("myDBField") %>'
This will take care of everything: showing the data and
updating the data after postback.
--
Riki
set dropdownlist selectedindex in datagrid
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
I have a templatecolumn as follows:
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
with an edit column as:
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel" EditText="Edit"></
asp:EditCommandColumn>
I would like to set the dropdownlist to the value found in the
database. my code behind uses ItemDataBound as follows:
public void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dd;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
}
}
upon loading the dropdownlist has the values as in the database.
However, when I press edit the selected index becomes 0. I cannot
figure how to set the value correctly.
my edit code looks like:
public void myDataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dd;
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
<-- dd.SelectedIndex looks fine here -->
...
}
dd.SelectedIndex looks ok in debug mode, but something changes it
before the dropdownlist is displayed. the values in the dropdownlists
not being edited are ok, it's only the one being edited that shows the
wrong selectedindex.
any help would be appreciated. thanks.pleaseexplaintome@.yahoo.com wrote:
> Hi all,
> I have a datagrid with a dropdownlist and would like to have the
> dropdownlist display a database value correctly while the grid is in
> edit mode.
> I have a templatecolumn as follows:
> <asp:TemplateColumn HeaderText="New Route">
> <HeaderStyle Width="0.5in"></HeaderStyle>
> <ItemTemplate>
> <asp:DropDownList id="DropDownList3" runat="server"
> AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
> <asp:ListItem Value="A">A</asp:ListItem>
> <asp:ListItem Value="B">B</asp:ListItem>
> <asp:ListItem Value="C">C</asp:ListItem>
> <asp:ListItem Value="D">D</asp:ListItem>
> </asp:DropDownList>
> </ItemTemplate>
There is a much easier way (at least in ASP.NET 2.0)
Set the property SelectedValue on the DrowDownlist
declaratively like this:
SelectedValue='<%# Bind("myDBField") %>'
This will take care of everything: showing the data and
updating the data after postback.
Riki
Saturday, March 24, 2012
Set Focus and post back on Dropdown List
The focus remains on the dropdown list after the postback and I am trying to get it away from it because the user may and does want to use the mouse wheel to scroll down, but since the focus is on the ddl, they may not see it and it will resort.
Here is my set focus code which works fine on page load event...
Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" & ctrl.ID & "').focus() </SCRIPT>"
RegisterStartupScript("focus", s)
End Sub
And this code is for the page load event...
If Not IsPostBack Then
Me.SetFocus(Me.txtSearch)
End If
I have been trying to place the Me.SetFocus(Me.txtSearch) where the postback of the ddl occurs in the code, such as the reBind() or the ddlTypes_SelectedIndexChanged() and the dgServer_ItemDataBound() which has the ddl code.
None seem to work, but it works on page load event.
Suggestions?
Thanks all,
Zath
This should work fine from your ddlTypes_SelectedIndexChanged() event handler. Are calling this method only if the IsPostBack property is false? If so, that could be your problem.
Have you tried putting the SetFocus in the Page_PreRender? This runs after the event code.
Private Sub Page_PreRender(ByVal sender AS Object,VyVal e AS System.EventArgs) Handles MyBase.PreRender
If ispostback=true then
Me.SetFocus(Me.txtSearch)
End if
End Sub
You can use
If Not ispostback
if necessary.
check out my component here :view post 821121
regards
Set focus on the textbox field in datagrid
when one record was added in the grid, the cursor was lost focus to
somewhere. is it possible to set focus on the textbox for further adding
record in the datagrid
Million thanksCheck out this articles, it might help you.
http://aspalliance.com/aldotnet/exa...autoscroll.aspx
http://www.dotnetjunkies.com/howto/default.aspx?id=38
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com
"Grey" <erickwyum@.i-cable.com> wrote in message
news:#A2nIPNVEHA.712@.TK2MSFTNGP11.phx.gbl...
> I have implemented the datagrid with add function successfully. However,
> when one record was added in the grid, the cursor was lost focus to
> somewhere. is it possible to set focus on the textbox for further adding
> record in the datagrid
> Million thanks
>
Set focus on the textbox field in datagrid
when one record was added in the grid, the cursor was lost focus to
somewhere. is it possible to set focus on the textbox for further adding
record in the datagrid
Million thanksCheck out this articles, it might help you.
http://aspalliance.com/aldotnet/exa...autoscroll.aspx
http://www.dotnetjunkies.com/howto/default.aspx?id=38
--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com
"Grey" <erickwyum@.i-cable.com> wrote in message
news:#A2nIPNVEHA.712@.TK2MSFTNGP11.phx.gbl...
> I have implemented the datagrid with add function successfully. However,
> when one record was added in the grid, the cursor was lost focus to
> somewhere. is it possible to set focus on the textbox for further adding
> record in the datagrid
> Million thanks
Set focus to DAtagrid Cell
columns with Dropdown lists. My Third column as databound cell. When I go
to edit a particular row I want the focus to change to the third row. I am
using Visual Studio VB.Net with WEB Forms. Any Help would be great.It is not clear what exactly you want. Anyway, setting focus is a
client-side task. You have to produce some javascript which will operate on
the client DHTML presentation of the grid.
Eliyahu
"rroca" <oeb@.homexperts.net> wrote in message
news:C3CD2487-6FD7-4830-A20D-E14E18CA56F1@.microsoft.com...
> I hae a WEB application that uses a datagrid. My first 2 rows are
Template
> columns with Dropdown lists. My Third column as databound cell. When I
go
> to edit a particular row I want the focus to change to the third row. I
am
> using Visual Studio VB.Net with WEB Forms. Any Help would be great.
Set focus to DAtagrid Cell
columns with Dropdown lists. My Third column as databound cell. When I go
to edit a particular row I want the focus to change to the third row. I am
using Visual Studio VB.Net with WEB Forms. Any Help would be great.It is not clear what exactly you want. Anyway, setting focus is a
client-side task. You have to produce some javascript which will operate on
the client DHTML presentation of the grid.
Eliyahu
"rroca" <oeb@.homexperts.net> wrote in message
news:C3CD2487-6FD7-4830-A20D-E14E18CA56F1@.microsoft.com...
> I hae a WEB application that uses a datagrid. My first 2 rows are
Template
> columns with Dropdown lists. My Third column as databound cell. When I
go
> to edit a particular row I want the focus to change to the third row. I
am
> using Visual Studio VB.Net with WEB Forms. Any Help would be great.
Thursday, March 22, 2012
Set Focus to User Control
all help appreciated, thanxImportant note:
Your usercontrol doesn't receive focus, because it doesn't render as an htmlcontrol that uniquely identifies it. It only exists on the server side inside the .Net framework. Once it gets to the client, 'it' is only the composition of the html that makes it useful.
Let's pretend that instead of your control, your using this control:
A control class called 'MyTextBox' which inside it contains:
An asp:textbox
An asp:RequiredFieldValidator
You also slap an ID on the asp:textbox of 'textboxAlpha'
And an ID on the RequiredFieldValidtor of 'rq1'
Now, when you throw your MyTextBox control on a webform, ASP.NET will probably give it an ID of 'MyTextBox1', and unless you rename the ID, it will take that by default.
Now... to access the textbox in this control from Javascript you would use:
document.getElementById('MyTextBox1_textboxAlpha');
That's easy enough.
Things get a little different when working with 'databound-controls'.
I know that for repeaters... to get to the contents of a textbox inside the repeater, you have to use 'repeater1_ctrl0_ctrl0'.
The first _ctrl0 part is actually the first listed dataitem, and the second _ctrl0 is the first control in that dataitem.
So for a repeater... if you had two items that were listed, and in the repeater item template you told it to render THREE textboxes with each item that gets listed, your repeater would have these controls:
repeater1_ctrl0_ctrl0
repeater1_ctrl0_ctrl1
repeater1_ctrl0_ctrl2
repeater1_ctrl1_ctrl0
repeater1_ctrl1_ctrl1
repeater1_ctrl1_ctrl2
Anyway, best thing to do is choose View Source after your page has rendered, and look what the DataGrid wrote for you. You'll be looking for an INPUT tag that would have the name of your Datagrid plus _ctrl0 or something like that.
Thanx for the reply but actually I still cant get this to work. As you can see the HTML output of the page is similar to this:
<div id="UcDtgMain_pnlMain" style="border-width:0px;border-style:Solid;Width:0;Height:226;">
<div id="UcDtgMain_pnlHeader" align="Center">
<span id="UcDtgMain_lblTitre" class="TitleStyle">Title of DataGrid</span>
</div>
<table class="DataGridStyle" cellspacing="0" id="UcDtgMain_DataGrid1">
<tr><td>Code</td><td>Description</td></tr>
<tr class="ItemStyle">
<td align="Right"><span id="UcDtgMain_DataGrid1__ctl2_lblCode"><acronym Title='1'>1</acronym></span></td><td align="Left"><span id="UcDtgMain_DataGrid1__ctl2_lblDescription"><acronym Title='Monsieur'>Monsieur</acronym></span></td></tr>
<tr class="AlternatingItemStyle" onclick="javascript:__doPostBack('UcDtgMain:DataGrid1:_ctl3:LinkButton1','')">
<td align="Right"><span id="UcDtgMain_DataGrid1__ctl3_lblCode"><acronym Title='2'>2</acronym></span></td><td align="Left"><span id="UcDtgMain_DataGrid1__ctl3_lblDescription"><acronym Title='Madame'>Madame</acronym></span></td>
</tr><tr class="ItemStyle" onclick="javascript:__doPostBack('UcDtgMain:DataGrid1:_ctl4:LinkButton1','')">
<td align="Right"><span id="UcDtgMain_DataGrid1__ctl4_lblCode"><acronym Title='3'>3</acronym></span></td><td align="Left"><span id="UcDtgMain_DataGrid1__ctl4_lblDescription"><acronym Title='Mademoiselle'>Mademoiselle</acronym></span></td>
</tr><tr class="AlternatingItemStyle" onclick="javascript:__doPostBack('UcDtgMain:DataGrid1:_ctl5:LinkButton1','')">
<td align="Right"><span id="UcDtgMain_DataGrid1__ctl5_lblCode"><acronym Title='4'>4</acronym></span></td><td align="Left"><span id="UcDtgMain_DataGrid1__ctl5_lblDescription"><acronym Title='Sir'>Sir</acronym></span></td>
</tr><tr class="ItemStyle" onclick="javascript:__doPostBack('UcDtgMain:DataGrid1:_ctl6:LinkButton1','')">
<td align="Right"><span id="UcDtgMain_DataGrid1__ctl6_lblCode"><acronym Title='5'>5</acronym></span></td><td align="Left"><span id="UcDtgMain_DataGrid1__ctl6_lblDescription"><acronym Title='Miss'>Miss</acronym></span></td>
</tr>
</table>
</div>
So basically there are a set on container controls that have been created and even if I use this:
Page.RegisterStartupScript("focus", _
"<script language=""JavaScript"">" & vbCrLf & vbTab & _
"document.getElementById(""UcDtgMain_DataGrid1__ctl2_lblCodeLIBCIVILITE"").focus();" & vbCrLf & _
"<" & "/script>")
it simply does not seem to work, am I missing something over here? I Think that maybe since the controls are finally interpreted as HTML tables then this might be the reason for this.
Well, spans can't receive focus. You have to have at least some control that can (usually ony type="INPUT" ), in your Item and AlternatingItem templates. Additionally, I'm pretty sure TR's cant receive events period, they must be TD's for onclicks... I could be wrong on that last part though.
Anyway, concerning the first part, I've seen people put a checkbox to select, which postsback and changes the TD (TD with a table and then a TR, and then TD's for each control in that datarow) background-color to mark it as selected, or simply throw a linkbutton that does nothing but simply display the Text in the span.
In fact, setting the datagrid's selected index to 1 would make the first row being selected and it would change in color, thus appearing to be selected. But since I use the arrow keys to navigate through the grid, it does not scroll up and down the grid until I perform a first click on the control. So maybe there is another work around for this.
Actually my datagrid user control is quite heavy in terms of functionalities, it even allows navigating through the control through arrow keys, allows paging to be done on Page Up and Page Down keys, column sorting and lots of other useful stuffs.
Anyway, I dont know if others have developed a similar control but it would have been good if they did. Maybe some of us could work on a more enhanced version of the datagrid control!!! Anybody wanna go for it? :rolleyes:
thanx again.
Set headertext in datagrid programatically
The dataadapter,dataset and databind() code runs in a button eventhandler
The headertext is currently determined by the column names of my SQL select
statement there.
How can I change the headertext programatically in the codebehind? ... I do
n't want to change the preexisting code if at all possible.in your itemdatabound event handler, you can test for the headeritem and
then use the text property attached to the cell object to change the text
e.item.cells[4].text = "vapor"
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"mg" <mg@.theworld.com> wrote in message
news:88A2B342-0B07-4CD5-80AC-8C989FE5C2EA@.microsoft.com...
>I create a datagrid using the Web Forms Toolbox
> The dataadapter,dataset and databind() code runs in a button eventhandler
> The headertext is currently determined by the column names of my SQL
> select statement there.
> How can I change the headertext programatically in the codebehind? ... I
> don't want to change the preexisting code if at all possible.
Set headertext in datagrid programatically
The dataadapter,dataset and databind() code runs in a button eventhandler
The headertext is currently determined by the column names of my SQL select statement there
How can I change the headertext programatically in the codebehind? ... I don't want to change the preexisting code if at all possible.in your itemdatabound event handler, you can test for the headeritem and
then use the text property attached to the cell object to change the text
e.item.cells[4].text = "vapor"
--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"mg" <mg@.theworld.com> wrote in message
news:88A2B342-0B07-4CD5-80AC-8C989FE5C2EA@.microsoft.com...
>I create a datagrid using the Web Forms Toolbox
> The dataadapter,dataset and databind() code runs in a button eventhandler
> The headertext is currently determined by the column names of my SQL
> select statement there.
> How can I change the headertext programatically in the codebehind? ... I
> don't want to change the preexisting code if at all possible.