IN dot net, User control has a drop down when it is changed. The grid needs to be changed, but the grid is not inside user control?
How can we achieve it?
You bind an event using the OnSelectedIndexChange to your drop down list on your aspx page:
<asp:DropDownList ID="ddlGridType" runat="server" OnSelectedIndexChanged="ddlGridType_SelectedIndexChanged" AutoPostBack="true" >
Then on your C# code behind:
protected void ddlRunType_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtblDataSource = New DataTable();
string gridType = ddlGridType.SelectedValue;
//remove the old data from the grid view
TheGridView.DataSource = null;
TheGridView.DataBind();
//get the new data using whatever method you use to get the data
dtblDataSource = theGridView_GetDataSource(gridType);
//bind the new data to the list
TheGridView.DataSource = dtblDataSource;
TheGridView.DataBind();
}
Related
i am using entity frame work and want to bind data on Grid View but facing problem i have code that i am pasting as well as attaching screen shot i also saw answer regarding this problem but not beneficial for me so any one have experience with this error must be appreciated.
aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
lblMessage.Text = "";
}
void BindGrid()
{
using (GapEntities1 context = new GapEntities1())
{
if (context.Organizations.Count() > 0)
{
// GdvOrganization is a gridview ID name
GdvOrganization.DataSource = context.Organizations;
GdvOrganization.DataBind();
}
}
}
Seems like you are trying to bind the GridView from Markup side (.aspx ) as well as using code behind.( .aspx.cs )
Choose any one way only to bind the grid.
1.) If you bind gridview from code behind then remove the DataSourceId property from grid view from markup. Change below code:
<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False"
DataSourceID="MyDataSource">
to
<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False">
2.) if you prefer to bind from markup side then you have to remove the c# code to bind the grid.
Still if above 2 steps doesn't interest you, try below trick ( Recommended ?? )
GdvOrganization.DataSource = ds;
GdvOrganization.DataSourceID = String.Empty;
GdvOrganization.DataBind();
I have developed a ASP.Net web application using adomd to display the ssas cube data.
I am able to bind the data into gridview but the problem is i am unable to display the header name in the gridview. I read through the below article too but no luck. does anyknow how to fix it.
http://www.codeproject.com/Articles/28290/Microsoft-Analysis-Services-2005-Displaying-a-grid
thanks
Use RowDataBound event of gridview. Then in the event handler check for header row and set appropriate header in appropriate cells. For example, say we have this gridview markup:
<asp:GridView runat="server" AutoGenerateColumns="True" ID="gv" OnRowDataBound="RowDataBound"></asp:GridView>
and, on page load we are binding data like:
var myData = Enumerable.Range(1, 5).Select(i => new { Value = i.ToString() });
gv.DataSource = myData;
gv.DataBind();
and in the rowdatabound event handler we can do this to change the header:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "My Custom Header";
}
}
Staying away from javascript I endeavor to use Check_Clicked event handler to populate my Shipping info if same as Billing info in my FormView. This should be real simple but I have not been able to get the plumbing right.
I am following the example in http://msdn.microsoft.com/en-us/library/4s78d0k1%28v=vs.71%29.aspx#Y617 but would like to use my FormView instead of Form1.
The value that appears on checking the box is System.Web.UI.WebControls.TextBox
<asp:CheckBox id="SameCheckBox" AutoPostBack="True"
Text="Same as billing." TextAlign="Right"
OnCheckedChanged="Check_Clicked" runat="server"/>
protected void Check_Clicked(Object sender, EventArgs e)
{
CheckBox SameCheckBox = (CheckBox)FormView1.FindControl("SameCheckBox");
TextBox BillingFirst = (TextBox)FormView1.FindControl("BillingFirstNameTextBox");
TextBox ShippingFirst = (TextBox)FormView1.FindControl("ShippingFirstNameTextBox");
if (SameCheckBox.Checked)
{
ShippingFirst.Text = BillingFirst.ToString();
}
else
ShippingFirst = null;
}
In addition to the solutions given to me below I will add for others edification; the other problem I had was DropdownList data. Here is what worked for me:
DropDownList BillingState = FormView1.Row.FindControl("BillingStateTextBox") as DropDownList;
DropDownList ShippingState = FormView1.Row.FindControl("ShippingStateTextBox")as DropDownList;
ShippingState.SelectedValue = BillingState.Text;
This line:
ShippingFirst.Text = BillingFirst.ToString();
Should be:
ShippingFirst.Text = BillingFirst.Text;
The ToString() output of a WebControl will be the type name.
Use :
ShippingFirst.Text = BillingFirst.Text;
I am using a Listview in a usercontrol that I databind to a list of object in the page load event.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
BindListViews();
}
private void BindListViews()
{
MyListView.DataSource = IncludeExpressions;
MyListView.DataBind();
}
I need to handle inserting new items in the list.
To do that, I added an InsertItemTemplate with a button that has "Insert" as command argument.
I dont want to persist the data to the database until the user press the save button, at the bottom of the form.
So in the ItemCommand event, Here is my code:
protected void Expression_ItemCommand(object sender, ListViewCommandEventArgs e)
{
var listView = (sender as ListView);
var expressions = GetExpressions(listView);
var newExpression = new Expression
{
CaseSensitive = ((CheckBox)e.Item.FindControl("CaseSensitiveCheckBox")).Checked,
SearchText = ((TextBox)e.Item.FindControl("SearchTextTextBox")).Text,
Scope = (Scope)Enum.Parse(typeof(Scope), ((DropDownList)e.Item.FindControl("ScopeDropDownList")).SelectedValue, true),
Type = (Type)Enum.Parse(typeof(Type), ((DropDownList)e.Item.FindControl("TypeDropDownList")).SelectedValue, true),
};
expressions.Add(newExpression);
listView.DataSource = expressions;
listView.DataBind();
UpdatePanelInclude.Update();
}
private List<Expression> GetExpressions(ListView lv)
{
var expressions = new List<Expression>();
foreach (var row in lv.Items)
{
var searchText = ((TextBox)row.FindControl("SearchTextTextBox")).Text;
...
expressions.Add(new Expression
{
CaseSensitive = caseSensitive,
Scope = scope,
Type = type,
SearchText = searchText
});
}
return expressions;
}
This works perfectly fine until I add an UpdatePanel around the listview.
When I add an updatepanel, the Expression_ItemCommand handler is hit only every 2 clicks, eventhough the page is post back every click.
While debugging, I can see that I do enter the Page_Load event of the page at each click on the Insert button, but it hits the Expression_ItemCommand only every 2 clicks. and reset the content of my listview when the ItemCommand is not hit.
I smell ViewState problems here, but I can't figure out how to fix it.
Here is what the markup looks like :
<asp:UpdatePanel ID="UpdatePanelInclude" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
<asp:ListView ID="MyListView" OnItemCommand="Expression_ItemCommand" OnItemInserting="ExpressionInserting" OnDataBinding="ListViewDataBinding" InsertItemPosition="LastItem" runat="server" ItemPlaceholderID="itemPlaceHolder">
...
...
Any Idea how to solve this?
Stéphane
After looooong investigations and rebuilding of the page control by control, the reason was that the viewstate was compressed and the scriptmanager didnt like it somehow, even though I specify the hidden field to it.
Probleme solved...
I have a page with a table of stuff and I need to allow the user to select rows to process. I've figured out how to add a column of check boxes to the table but I can't seem to figure out how to test if they are checked when the form is submitted. If they were static elements, I'd be able to just check do this.theCheckBox but they are programaticly generated.
Also I'm not very happy with how I'm attaching my data to them (by stuffing it in there ID property).
I'm not sure if it's relevant but I'm looking at a bit of a catch-22 as I need to known which of the checkboxes that were created last time around were checked before I can re-run the code that created them.
Edit:
I've found an almost solution. By setting the AutoPostBack property and the CheckedChanged event:
checkbox.AutoPostBack = false;
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
I can get code to be called on a post back for any check box that has changed. However this has two problems:
The call back is processed after (or during, I'm not sure) Page_Load where I need to use this information
The call back is not called for check boxes that were checked when the page loaded and still are.
Edit 2:
What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:
foreach (string v in this.Request.Form.AllKeys)
{
if (v.StartsWith(Prefix))
{
var data = v.Substring(Prefix.Length);
}
}
everything else seems to run to late.
I'm going to assume you're using a DataList but this should work with and Control that can be templated. I'm also going to assume you're using DataBinding.
Code Front:
<asp:DataList ID="List" OnItemDataBound="List_ItemDataBound" runat="server">
<ItemTemplate>
<asp:CheckBox ID="DeleteMe" runat="server"/>
<a href="<%# DataBinder.Eval(Container, "DataItem.Url")%>" target="_blank">
<%# DataBinder.Eval(Container, "DataItem.Title")%></a>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="DeleteListItem" runat="server" OnClick="DeleteListItem_Click" ></asp:Button>
Code Behind:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadList();
}
protected void DeleteListItem_Click(object sender, EventArgs e)
{
foreach (DataListItem li in List.Items)
{
CheckBox delMe = (CheckBox)li.FindControl("DeleteMe");
if (delMe != null && delMe.Checked)
//Do Something
}
}
LoadList();
}
protected void LoadList()
{
DataTable dt = //Something...
List.DataSource = dt;
List.DataBind();
}
protected void List_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string id = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
CheckBox delMe = (CheckBox)e.Item.FindControl("DeleteMe");
if (delMe != null)
delMe.Attributes.Add("value", id);
}
}
}
First, make sure that each Checkbox has an ID and that it's got the 'runat="server"' in the tag.
then use the FindControl() function to find it.
For example, if you're looping through all rows in a GridView..
foreach(GridViewRow r in Gridview1.Rows)
{
object cb = r.FindControl("MyCheckBoxId");
if(r != null)
{
CheckBox chk = (CheckBox)cb;
bool IsChecked = chk.Checked;
}
}
Postback data is restored between the InitComplete event and the PreLoad event. If your checkboxes are not created until later then the checkboxes will play "catch up" with their events and the data will be loaded into the control shortly after it is created.
If this is to late for you then you will have to do something like what you are already doing. That is you will have to access the post data before it is given to the control.
If you can save the UniqueId of each CheckBox that you create then can directly access the post data without having to given them a special prefix. You could do this by creating a list of strings which you save the ids in as you generate them and then saving them in the view state. Of course that requires the view state to be enabled and takes up more space in the viewstate.
foreach (string uniqueId in UniqueIds)
{
bool data = Convert.ToBoolean(Request.Form[uniqueId]);
//...
}
Your post is a little vague. It would help to see how you're adding controls to the table. Is it an ASP:Table or a regular HTML table (presumably with a runat="server" attribute since you've successfully added items to it)?
If you intend to let the user make a bunch of selections, then hit a "Submit" button, whereupon you'll process each row based on which row is checked, then you should not be handling the CheckChanged event. Otherwise, as you've noticed, you'll be causing a postback each time and it won't process any of the other checkboxes. So when you create the CheckBox do not set the eventhandler so it doesn't cause a postback.
In your submit button's eventhandler you would loop through each table row, cell, then determine whether the cell's children control contained a checkbox.
I would suggest not using a table. From what you're describing perhaps a GridView or DataList is a better option.
EDIT: here's a simple example to demonstrate. You should be able to get this working in a new project to test out.
Markup
<form id="form1" runat="server">
<div>
<table id="tbl" runat="server"></table>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</div>
</form>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
var row = new HtmlTableRow();
var cell = new HtmlTableCell();
cell.InnerText = "Row: " + i.ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
CheckBox chk = new CheckBox() { ID = "chk" + i.ToString() };
cell.Controls.Add(chk);
row.Cells.Add(cell);
tbl.Rows.Add(row);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (HtmlTableRow row in tbl.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
foreach (Control c in cell.Controls)
{
if (c is CheckBox)
{
// do your processing here
CheckBox chk = c as CheckBox;
if (chk.Checked)
{
Response.Write(chk.ID + " was checked <br />");
}
}
}
}
}
}
What about using the CheckBoxList control? I have no Visual Studio open now, but as far as I remember it is a DataBound control, providing DataSource and DataBind() where you can provide a list at runtime. When the page does a postback you can traverse the list by calling something like myCheckBoxList.Items and check whether the current item is selected by calling ListItem.Selected method. This should work.
Add them in an override of the CreateChildControls method of the Page. Be sure to give them an ID! This way they get added to the control tree at the correct time.
IMHO The best way would be to use DataBound Templated Control though, i.e. something like a ListView (in .NET 3.5). then in pageload after postback traverse all items in the databound control and use item.FindControl to get at the actual checkbox.
What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:
foreach (string v in this.Request.Form.AllKeys)
{
if (v.StartsWith(Prefix))
{
var data = v.Substring(Prefix.Length);
}
}
everything else seems to run to late.