I have a page of an application that allows the user to select a value from a DropDownList control and populate a GridView on that basis.
What I want is if the page is loaded with a parameter set (probably via query string, this is an internal business system) for the GridView to be populated using that as the SelectedValue.
Here's the ASP:
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<%-- Drop down lost of available claims --%>
<asp:Label ID="ClaimSelectionLabel" AssociatedControlID="ClaimsListDropDown" Text="Select a Claim to see batches" runat="server" />
<asp:DropDownList ID="ClaimsListDropDown" runat="server" AutoPostBack="true"
SelectMethod="GetClaims" ItemType="CO_GAVIN.Data.DAL.EntityFramework.Claim"
DataTextField="ClaimName" DataValueField="ClaimRef"></asp:DropDownList>
<%-- Table of Batches for a Claim determined by ClaimRef optional param passed in,
if no param then blank, selection from ClaimsListDropDown --%>
<asp:GridView ID="BatchReviewGrid" runat="server" ItemType="CO_GAVIN.Data.DAL.EntityFramework.ClaimBatch"
SelectMethod="GetBatchesInClaim" DataKeyNames="ClaimRef" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="BatchName" HeaderText="Batch Name" />
<asp:BoundField DataField="TotalDonations" HeaderText="Total Donations" />
<asp:BoundField DataField="AmountClaimed" HeaderText="Amount Claimed" />
<asp:BoundField DataField="EarliestDate" HeaderText="Begin Date" />
<asp:BoundField DataField="LatestDate" HeaderText="End Date" />
</Columns>
</asp:GridView>
</asp:Content>
And here's the code behind:
public partial class ReviewBatches : System.Web.UI.Page
{
private GavinCharitiesOnlineEntities _db = new GavinCharitiesOnlineEntities();
public IQueryable<Claim> GetClaims()
{
var query =
_db.Claims
.Where(c => c.ClaimStatus == "STARTED");
return query;
}
public IQueryable<ClaimBatch> GetBatchesInClaim([Control("ClaimsListDropDown")] int? ClaimRef)
{
var query = _db.ClaimBatches
.Where(cb => cb.ClaimRef == ClaimRef);
return query;
}
protected void Page_Load(object sender, EventArgs e)
{
int ClaimRef = 0;
if (!Page.IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["ClaimRef"]))
{
ClaimRef = Convert.ToInt32(Request.QueryString["ClaimRef"]);
ClaimsListDropDown.SelectedValue = ClaimRef.ToString();
}
}
}
}
I've tried to set the DataSource property of the GridView in the Page_Load event but then I get an error about "datasource and datasourceid cannot be defined on BatchReviewGrid when when it uses model binding".
I was thinking I might just create a new DataGrid to handle the case where a param is passed in, and keep it hidden if no param is passed but that seems sub-optimal.
I have set the dropdownlist to take the param as its selected value if one is present on page_load - can I not simply force the gridview to render using this?
UPDATE: Following @workabyte's suggestion I have changed my code to use the OnInit method to set the SelectedValue property of the DropDownList if the QueryString parameter is set.
In order to get this to work I added an ObjectDataSource and set this to use a method from a repository I created as per this tutorial http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control,-part-1-getting-started
I now have a page that works much better in that the GridView responds instantly if the page is loaded with the ClaimRef parameter set.
My last remaining problem is to make sure if the page is called WITHOUT a parameter, the GridView is empty. At present, the default is the first item in the DropDownList. I have tried clearing the selection using this:
protected override void OnInit(EventArgs e)
{
int ClaimRef = 0;
if (!Page.IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["ClaimRef"]))
{
ClaimRef = Convert.ToInt32(Request.QueryString["ClaimRef"]);
ClaimsListDropDown.SelectedValue = ClaimRef.ToString();
}
else
{
ClaimsListDropDown.ClearSelection();
}
}
base.OnInit(e);
}
But it doesn't work - no errors, it just doesn't work. If I use:
ClaimsListDropDown.SelectedIndex = 1;
Then the second item in the list (index 1) is chosen when the page loads. Setting the SelectedIndex to -1 doesn't work. I also tried this:
ClaimsListDropDown.DataBind();
ClaimsListDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ClaimsListDropDown.SelectedIndex = 0;
But that doesn't work either. Does anyone have any suggestions for setting the DropDownList so it has no value if there is no QueryString param and therefore the GridView is empty on pageload?
i think you can override the oninit and then set the value of the drop down there. by the time the life cycle get to the databind for the data source assuming that you have a control parameter tied to the drop down. the data source should just work as expected using the value you have set.
hope this helps
for setting the dropdown to have an empty default item this is what i generally do
<asp:dropdown .... AppendDataBoundItems=true>
<asp:item value="">-- select one--</asp:item>
</asp:dropdown>
naturally that needs some cleanup but you should get the gist of it