I have set a dynamic gridview, which i want to use it in another form so i created it as
public GridView gv = new GridView()
i have set in Page_Load properties of gv to
gv.AutoGenerateColumns = true;
gv.Visible = true;
then i have another grid MyGridView which is static and have data in it. So when i try to copy all static gridview data to dynamic gridview data that is
gv.DataSource = MyGridView.DataSource;
gv.DataBind();
I am not able to view the Grid gv, why is it so? someone told me to add
'Controls.Add(gv) in Page_Load
when i added its showing compile time error, that gv should be in a form with runat=server.
How can i put a dynamic gridview into a form with runat=server?
So my requirement is gv should be visible, how can i achieve this?
Thanks in advance.
you need to use some sort of holders for this try pannel
on aspx page create
<asp:Panel id="panel1" runat="server"></asp:Panel>
on your backednd
do
panel1.Controls.Add(gv)
Place a div with runat="server" into your page
then:
YourDivID.Controls.Add(gv)
Related
I have written a method that retrieves data from the database and returns a datatable comprising of three columns.
This datatable I am binding to a gridview control after hiding the ID field.
DataTable dt = _qbObj.getAllTags();
dvTags.DataSource = dt;
BoundField bfName = new BoundField();
bfName.DataField = dt.Columns["Name"].ToString();
bfName.HeaderText = "Name";
BoundField bfId = new BoundField();
bfId.DataField = dt.Columns["ID"].ToString();
bfId.Visible = false;
BoundField bfDesc = new BoundField();
bfDesc.DataField = dt.Columns["Description"].ToString();
bfDesc.HeaderText = "Description";
dvTags.Columns.Add(bfId);
dvTags.Columns.Add(bfName);
dvTags.Columns.Add(bfDesc);
dvTags.DataBind();
To this gridview control, I want to add an edit button, which should pop-up a jquery modal dialog box where I can enter the updated details.
I realize that I can go with a , but the problem is that I need to pop that modal dialog box without refreshing the page, and the doesn't exactly have great support for scripting.
So inside my gridview I inserted this, an Item Template.
<asp:GridView ID="dvTags" runat="server" CssClass="labs-grid-view"
AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="dvTagEdit" CommandName="UpdateRecord"
CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now after I am done editing the gridview doesn't update automatically, and hence I have a dedicated refresh grid button which deletes the existing "dynamically inserted columns" using this code :-
int noOfRows = dvTags.Columns.Count;
if (noOfRows > 1)
{
dvTags.Columns.RemoveAt(noOfRows - 1);
dvTags.Columns.RemoveAt(noOfRows - 2);
dvTags.Columns.RemoveAt(noOfRows - 3);
// THERE ARE A TOTAL OF **THREE** COLUMNS
}
But the problem is that after refreshing the page a couple of times, my button inside the ItemTemplate disappears and in the html is replaced with an " "
Please help me find the error. I'm thinking there is a better and easier way to achieve this. If so I'm open to them.
Thanks for reading,
Abijeet.
A few things to consider:
First, can you post your code behind for your ItemCommand event, which is rendering the modal popup and which is performing the update? It could be that your refresh after edit is not processing properly.
Second, instead of doing your databinding "inline" on your GridView, consider using the RowDataBound event within the Gridview. You can detect which row is being generated (header, data, footer) and you can properly create your edit button in there. Better yet, you can access your button from within this method and simply set the CommandArgument to your Id.
Third, when using the asp button in your GridView, it will trigger the "ItemCommand" event when clicked, which will cause a postback.
I'd recommend having a simple link in your template column, or something that you can use to trigger a jQuery modal, and you can setup a static naming convention for your items that will properly retrieve the data to put into your modal popup for editing. Then from there you should be able to process your update as normal.
I hope something in here helps.
I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link
I am trying to access a DropDownList within a div after I add them from the code-behind.
If I add DropDownList to my .aspx page like this:
<div id="FiltersDiv" runat="server">
<asp:DropDownList ID="DropDownListMenu" runat="server"
DataTextField="name" DataValueField="id"
AutoPostBack="true"
onselectedindexchanged="DropDownListMenu_SelectedIndexChanged" >
</asp:DropDownList>
I can get access in any event
protected void AddButton_Click(object sender, EventArgs e)
{
var dl = addProductLocationFiltersDiv.Controls.OfType<DropDownList>();
}
but not if I add from code
var ddl = new DropDownList();
ddl.DataSource = flist;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.DataBind();
FiltersDiv.Controls.Add(ddl);
I need to add several DropDownList’s and I do not now how many will be. So, I need to looping thru.
Did I miss some attribute or how I need to do it?
When you add a control via code, you access it through the variable name.
If you need access to several of these, consider putting then in a List<DropDownList> (or List<Control> if you have different types of controls).
If you need to share these across methods in your code behind, use a field instead of a local variable.
Note, that due to how the ASP.NET page lifecycle works, you will probably need to recreate these on every postback, otherwise you will not be able to access them. The best location to recreate such controls dynamically is in the OnInit handler.
foreach(DropDownList ddl in FiltersDiv.Controls.OfType<DropDownList>())
{
Trace.Write(ddl.SelectedValue);
}
This is off the top of my head, so you may need to cast the FiltersDiv to a drop down list - As said, you'll need to make sure the controls have been recreated as soon as possible on the page before the control data is written or read.
If you try this too soon, or havn't created the controls you'll get no results.
I am using a Gridview with datasource is a List. How can I show the header if the List is null or for a empty gridview?
if you are using fx4.0 then Set ShowHeaderWhenEmpty to true in Grid view
please look at below code
asp:GridView ID="gvPreview" runat="server" ShowHeaderWhenEmpty="True"
See A more elegant solution to display GridView header and footer when the data source is empty.
You can use HeaderTemplate property to setup the head programatically or use ListView instead if you are using .NET 3.5.
or
You can even try below
//if data exists
if (dtSource.Rows.Count != 0)
{
grdView.DataSource = dtSource;
grdView.DataBind();
}
else
{
//Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
dtFunding.Rows.Add(dtSource.NewRow());
grdView.DataSource = dtSource;
grdView.DataBind();
grdView.Rows[0].Visible = false;
}
I have a situation where I currently have a HyperLinkColumn control that I would like to modify to have a Label or simple text appear in the same column as the hyperlink. How do I acheive this please? Any Idea?
If this is DataGrid then you can handle the ItemCreated event and add code that adds a new control. For example:
<asp:DataGrid ... OnItemCreated="OnMyDataGridItemCreated" ... />
private void OnMyDataGridItemCreated(object sender, DataGridItemEventArgs e) {
Label textLabel = new Label();
textLabel.Text = "Hello!";
e.Item.Cells[3].Controls.Add(textLabel);
// Instead of "3" you might need to pick a different column
}
If you are using a GridView you could use a TemplateField. In there, you can have a hyperlink and a label, which are both databound. In the OnRowDataBound() Event you can decide which of the two controls should be visible to the user.
Edit:
I've written an article about different ways to bind data to a GridView and decided to add an example of a HyperLink and Label, whose properties are set in the RowDataBound-Event: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns