Invalid Postback On Server Populated DropDownLists - c#

I've seen multiple variations of this question asked, but all of the answers seem to involve javascript.
I'm querying a database, retrieving a list of items, and populating 3 different dropdown lists with the exact same content. After I select an item from a list, and click the "Save" button, I get an invalid postback error.
I'm certain it's because I'm using AutoPostBack on my DropDownList to trigger an update and disable a new selection in the other lists. When I set AutoPostBack to false, the error disappears.
I've read that I need to register my list content for event validation, but the examples I'm seeing, as mentioned above, are manually entered or handled with javascript; not server side from content generated by a DB query.
The solution I'm using now is a "link button" instead of a regular asp:button, since the former does not seem to cause a postback.
I would like to know if there is a more formal way of handling this. I'm not going to disable event validation, for security reasons.
<asp:UpdatePanel ID="DropDownListPanel" runat="server"
ChildrenAsTriggers="true" ><ContentTemplate>
<asp:gridview GridLines="Both" runat="server"
ShowHeader="false" ShowFooter="true" HorizontalAlign="Left"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ControlStyle-Height="100%" ItemStyle-Width="25%">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="true" Width="100%" AutoPostBack="true"
OnSelectedIndexChanged="CheckItem">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</Columns>
</asp:gridview>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="SaveButton" runat="server" Text="Save"
Enabled="True" OnClick="SaveTable" />

Related

EditItemTemplate and InsertItemTemplate wont allow dropdownlist designer entry

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>

ASP.NET C# checbox select after searching gridview

I have a problem with update panel which contains gridview (dynamically generated). Every row in gridview contains checbox, name of person and surname of person.
When user click on close button then every value (name) of selected row is saved in cookie (using jquery and asp.net-hidden value). That is working all right.
Problem appears here:
I also have possibility to search after name or surname of persons, becuase there are a lot of persons and that should help user to quickly find a right person.
Let say that I have this data:
Checbox1 John Doe
Checkox2 Michael Clark
Checbox1 (John Doe) is already selected.
So when I typed Clark in Surname textbox and click button "Search" gridview is generated again and there is one row (Michael Clark). When user select checbox of Michael Clark and click close button than cookie has only one value (Michael Clark, John Doe is logically missing).
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" DefaultButton="isciPodjetja">
<asp:TextBox ID="namePerson" runat="server" />
<asp:TextBox ID="surnamePerson" runat="server" />
<asp:Button ID="Searchperson" runat="server" Text="Search" OnClick="Serachperson_Click" />
</asp:Panel>
<p />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SelectALL">
<HeaderTemplate>
<asp:CheckBox ID="chkBxHeader" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" OnCheckedChanged="chkSelected_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NAME" HeaderText="Name of person"></asp:BoundField>
<asp:BoundField DataField="SURNAME" HeaderText="Surname of person" </asp:BoundField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Any Idea how can I implement that searching more easily? I tried with adding OnCheckedChanged to checbox, but autopostback does not work fine (when I clicked on checbox it is checked for a second, then is unchecked again).
Best solution for me would be, that when user click on search button than just hover appear on a correct row. Is it possible?
You have to develop two function in javascript begin request and end request.You have to write your javascript or jquery function in onbeginrequest() and onendrequest(), then only it will be fired. Please check it.
I solved my problem. I used javascript quicksearch plugin from https://github.com/riklomas/quicksearch.

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!

Troubles retrieving data from within OnRowCommand from a GridViews

If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.
I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...
Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources