EditItemTemplate and InsertItemTemplate wont allow dropdownlist designer entry - c#

I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!
<asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
<EditItemTemplate>
<asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
<asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Related

asp:ListView SelectedIndexChanging not firing

I've searched in google and bing and seen all posts and even in stackoverflows search but couldn't find the solution.
To be sure about the error I've created a new project it has 1 page which has:
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label><br/>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource" SelectCommand="Select * FROM Users" ConnectionString="<%$ConnectionStrings:DefaultConnection %>"></asp:SqlDataSource>
Output:
Because I've 2 users with name A and B.
Now the problem is that events OnSelectedIndexChanged and OnSelectedIndexChanging are not firing when I click on A or B. How to get the selected index where user is clicking?
Complete Project if anyone wants to look, actually there is no need to: https://app.box.com/s/bhs682q6fqai784kao0zv1tcxgxecr4s
ListView.SelectedIndexChanging Event occurs when an item's Select button is clicked, but before the ListView control handles the select operation.
(A Select button is a button control whose CommandName property is set to "Select".)
Change your code to
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging" AutoPostBack="true">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label>
<asp:LinkButton runat="server" ID="ButtonSelect" CommandName="Select" Text="SelectButton"/><br/>
</ItemTemplate>
</asp:ListView>

Unable to edit Asp.net DropDownList from code behind

I'm working on an ASP.net Web Forms application (not MVC) and I have a DropDownList wrapped by a TemplateField in a DetailsView like this:
<asp:TemplateField>
<ItemTemplate> </ItemTemplate>
<EditItemTemplate> </EditItemTemplate>
<InsertItemTemplate>
<div class="form-group">
<asp:DropDownList ID="myStatus" runat="server" DataSourceID="DBStatus"
DataTextField="DESC" DataValueField="CODE"
Enabled="false" SelectedValue='<%# Bind("Status") %>'>
</asp:DropDownList>
</div>
</InsertItemTemplate>
</asp:TemplateField>
as you can see at insertion its not enabled (Enabled="false")
for simplicity I wanted to use the insertion items/fields to achieve a search
so I want to enable my DropDownList for that search
I tried using these from code-behind on DetailsView_PreRender and Page_Load but nothing worked:
DropDownList Status = DetailsView1.FindControl("myStatus") as DropDownList;
Status.Enabled = true;
and i also tried
Status.Attributes.Add("enabled", "enabled");
i also debugged it, and these commands are executed but the DropDownList doesn't want to be enabled.
any thoughts ?
thanks.

HtmlInputCheckbox onselect event not getting invoked

I have a web application which has a gridview with first column as checkbox.
<asp:TemplateField HeaderText="Select" >
<HeaderTemplate >
<asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<input id="chkSelect" name="chkboxSelect" runat="server" type="checkbox" onclick='CheckedChanged'/>
</ItemTemplate>
</asp:TemplateField>
And also there is a onselect event for input(checkbox) written in code behind where I will highlight the row based on whether the checkbox is checked or not.
My problem is when I check the input checkbox, the onselect event is not getting invoked. Can anyone know whats causing this issue?
Thanks in advance.
Use asp:checkbox with autopostback=true instead of HTML control
use this code.
replace html input to asp:checkbox on itemtemplate and assign new event OnCheckedChanged instead of onclick event
<asp:TemplateField HeaderText="Select" >
<HeaderTemplate >
<asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged='CheckedChanged'/>
</ItemTemplate>

Eval() in a DropDownList within a GridView

How can I do Eval() in a DropDownList that are inside a GridView?
I tried the follow code, but still wrong:
<asp:TemplateField HeaderText="Prioridade">
<ItemTemplate>
<asp:DropDownList ID="ddlPrioridade" runat="server"
SelectedValue='<%# Eval("Prioridade") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Try this way
<asp:DropDownList ID="ddlPrioridade" runat="server" AppendDataBoundItems="true" DataSourceID="datasource" DataTextField="key" DataValueField="data" AutoPostBack="True" SelectedValue='<%# Eval("Prioridade") %>'>
also have a look at this DropDownList inside a GridView (or DataGrid). It might help you
You are receiving this error because the drop down list is not loaded with items and asp.net is trying to set the selected value of an empty drop down list and that's why it gives you error. So fill the drop down list first before setting it's selected value.

asp.net with c#

I have one page which is placed in the Master Page.
In the master page I have 1 dropdown and one GridView, dropdown is display the category, based on the dropdown list selection it will display the list of videos in the Grid.
In the content page I have the video player, in the page load it will play the video by default.
But when I choose the drop down list which is available in the master page, the page is refreshing, SO the video is start play from the first.
The content page should not refresh, So the video will continuously play.
How can I stop the page refresh in the content page?
All are in master page.
`<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<asp:DropDownList ID="drp_Channel" Width="220px" CssClass="ddl"
AutoPostBack="true" runat="server"
onselectedindexchanged="drp_Channel_SelectedIndexChanged">
<asp:ListItem>-- Select Channels --</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<asp:GridView ID="grd_Video" runat="server" AutoGenerateColumns="False" OnRowCommand="LinkName"
GridLines="None" ShowHeader="False" Width="100%" EmptyDataText="No Videos Found" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img_Video" runat="server" BorderColor="#666699" CssClass="imgbox"
ImageUrl='<%#(string)FormatImageUrl((string)Eval("Video_Thumbnail")) %>'
CommandName="imgClick" CommandArgument='<%# Bind("Video_ID")%>'
BorderWidth="0px" Height="40px" ToolTip="Click to view video" Width="50px"
BorderStyle="Double" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnk_VideoName" runat="server" ToolTip="Click to view video"
CommandName="lnkClick" CommandArgument='<%# Bind("Video_ID")%>'
Text='<%# DataBinder.Eval(Container, "DataItem.Video_Name") %>'
CssClass="linkVideo" Width="130px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#cccccc" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>`
in code behind for binding grid,
DataTable dt1Video = new MDBusiness.MDSUser().GetVideo(intNetId, intChanId, intCatId);
grd_Video.DataSource = dt1Video;
grd_Video.DataBind();
up1.Update();
up2.Update();
Set AutoPostBack = "False" on your dropdownlist to stop the postback from occurring. Users will then be able to change the dropdownlist without anything happening.
You'll need to use AJAX with your dropdownlist if you want to be able to use it without the postback.
for a quick answer
Install ASP.NET AJAX library
Wrap the GridView in a UpdatePanel
Set the trigger to be the DropDownList
Done :)
Remember to see this Screencast... it is exactly what you are after!

Categories

Resources