How to add controls to ReportViewer? - asp.net

I want to create report with my own controls, But the controls don't appear in the report!
I tried this:
GridView1.DataSource=(Order.GetDailyReport());
GridView1.DataBind();
ReportViewer1.Controls.Add(GridView1);
But nothing appeared!

Read this documentation
Customize the Report Viewer Web Part
But why would you want to add a gridView anyway? it is more meant for controls like the buttons you see on the toolbar.
I think you are trying to build it dynamically because the reportviewer comes with its own table.
You just need to change the way you populate the datasource:
Here is an example of how you would achieve that:
DataTable dt = Order.GetDailyReport();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
No need for gridview

Related

Dynamically adding Textbox in asp.net using C#

I have written a code in which a textbox is dynamically added to a gridview cell. There is some default texts in the textbox. I want that when users will click on the textbox the default text will disappear and the user can then write anything on it only in number, i.e. the user will not be able to use letters or special characters.
Kindly let me know how to achieve this.
Code example
Gridview gv=new Gridview();
gv.DataSource=dt;
gv.DataBind();
Textbox t1 = new Textbox();
t1.Text="Outages if any(in mins)";
gv.Rows[0].Cells[0].Controls.Add(t1);
Need help after this, something like when user puts his cursor in the textbox , the default text will disappear , and if the user removes the cursor without writing anything , the default text will reappear. Also the default text should be a bit blurred
Thanks.
Try something like this
Textbox t1 = new Textbox();
t1.Attributes.Add("onclick", "if(this.value == 'default text') this.value = '';"
t1.Attributes.Add("onblur", "if(this.value == '') this.value = 'default text';" />
You could also use onfocus in case users use tab key
With this, using this.value, approach you don't need to know the client ID of the control.
Here is a post describing exactly what you are looking to do:
HowTo: including default text in a Textbox while enforcing server-side validation
The pertinent points are:
Adding javascript attribute to onfocus & and onblur:
txtName.Attributes.Add("onfocus","clearText()");
txtName.Attributes.Add("onblur","resetText()");
Adding the javascript to clear and repopulate the textbox:
function clearText() {
document.form1.txtName.value = ""
}
function resetText() {
if(document.form1.txtName.value == "")
document.form1.txtName.value = "(enter something here)"
You can do this. The easiest way is to use the ajaxControlToolkit. You can create controls dynamically. For example:
Dim mt As new TextBox
Dim newTest As New AjaxControlToolkit.TextBoxWatermarkExtender
With mt
.ID = "textBox1"
.TextMode = TextBoxMode.SingleLine
End With
With newTest
.ID = "TextBoxWatermarkExtender1"
.TargetControlID = mt.ClientID
.WatermarkText = "test"
End With
Then just add both controls to the gridview as you are doing with the textbox. If you are not using Ajax, you can add javascript to the control through codebehind but this is more difficult. Let me know if that is what you want to do and Ill add some code to show you how.
This should create a textbox control with an associated ajax TextBoxWaterMarkExtender.

dynamic gridview in not visible / not showing

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)

pass data from code behind to jquery dialog

I am using below code to show jquery dialog, its working perfectly,
Sub OpenDialog(ByVal dialogId As String)
ScriptManager.RegisterStartupScript(Me, [GetType](), "openDialog", "$('#newPerson').dialog('open');", True)
End Sub
problem is that i wanted to pass some data from codebehind, i am using repeater control and binding repeater before opening dialog. it doesnt show any data on first click, when i click twice it show data in repeater control. any suggestion would be appreciated.
Regards
Use asp hidden fields,
Set there values in back end using this
HiddenField1.Value = "Your data";
& get them in JavaScript using this
document.getElementById('<%= HiddenField1.ClientID %>');

Calling the RowUpdate Method for dynamically created GridView

I have dynamically created a grid inside another grid. The parent grid is a static one created in aspx page and the child grid is created dynamically. Now, when a user clicks on the "Save" button in the Child grid I want the RowUpdating method to be triggered. Since I have created the Columns of the Child grid using TemplateGridView, I defined the Command name as follows in the TemplateGridView:
case ListItemType.Footer:
ImageButton BtnSave = new ImageButton();
BtnSave.ID = "Btn" + _columnName;
BtnSave.ImageUrl = "~/Images/saveIcon.jpg";
BtnSave.CommandName = "Update";
BtnSave.CausesValidation = true;
container.Controls.Add(BtnSave);
break;
Also, called the RowUpdating function where the ChildGrid's is created.
Tried the same with the RowCommand Argument too. But didn't work.
Kindly somebody help me on how to trigger an rowUpdate or rowcommand for a dynamically created grid.
NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound);
NewDg.RowUpdating += new GridViewUpdateEventHandler(NewDg_RowUpdating);
NewDg.RowEditing += new GridViewEditEventHandler(NewDg_RowEditing);
NewDg.RowCommand += new GridViewCommandEventHandler(NewDg_RowCommand);
Thanks in advance.
Try using a CommandField for the button column instead of a TemplateField. That's probably the easiest solution.
I believe the RowUpdating event is fired when the CommandName is something like Update$RowIndex.
Where do you add the EventHandlers? Remember that for dynamically created controls, EventHandlers should be added in Page_Init/PreInit in order for .net to see the event being called.

show gridview headers when binding the gridview with a list

I would like to show headers of a gridview which contains no data:
List<myData> datas = new List<myData>();
Gridview1.DataSource = datas.ToArray();
Gridview1.DataBind();
But then headers will be not shown. How can I show the headers in case the datas is empty?
I am still using .NET Framework 2.0, the server does not support .NET 4.0
Thanks in advance.
Check this one:
Show Grid view header and footer when the grid view is empty (with Generic List)
from your code I think the .ToArray() call is not needed, a GridView should be able to bind also to the List directly.
Use the GridView EmptyDataTemplate to display the headers when there's no data.
example:
<asp:GridView>
<emptydatatemplate>
<table><tr><td>Header 1</td><td>Header 2</td><td>Header 3</td><td>Header 4</td></tr></table>
</emptydatatemplate>
</asp:GridView>
you could try something like this...
//Check to see if we get rows back, if we do just bind.
if (grdview.Rows.Count != 0)
{
grdview.DataSource = dtFunding;
grdview.DataBind();
}
else
{
//Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
grdview.Rows.Add(grdview.NewRow());
grdview.DataSource = dtFunding;
grdview.DataBind();
grdview.Rows[0].Visible = false;
}

Resources