Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

Monday, March 26, 2012

set dropdownlist selectedindex in datagrid

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>

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

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>
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 on the textbox field in datagrid

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 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

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 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