pass data from code behind to jquery dialog - asp.net

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 %>');

Related

How to add controls to ReportViewer?

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

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.

Loop Through Controls on Web User Control

I'm having problems looping through controls that are on my user control.
I have tried the following code, but cannot get it to find the checkboxes that are on the user control. (You can see some of my previous attempts that I have commented out.)
'For Each Ctrl As Control In Page.Controls
'For Each Ctrl As Control In Me.Page.Controls
'For Each ctrl As Control In Request.Form
'''Dim frm As Control = Me.FindControl("frmDefault")
'''For Each Ctrl As Control In frm.Controls
Dim Check As CheckBox
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is CheckBox Then
Check = Ctrl
' Do something here...
End If
Next
There are multiple chekcboxes on the user control. The code shown above is on the code behind page for the user control.
(The user control is being used in conjunction with my CMS, Sitecore. I'm not sure if this has any effect on the problem I am experiencing or not.)
Any suggestions?
Sitecore has no effect on walking through the control-collection, this should be possible.
Are you looping through the right Control-Collection? Is Me.Controls your Page-, UserControl- or RepeaterItems-Control collection (or another collection)?
If the checkboxes are nested in another control, you need to walk to that control-collection.
Maybe you should add your .ascx code so we can see what your control-collection looks like.
Does this bring up the names of the checkboxes?
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is CheckBox Then
MsgBox(Ctrl.Name)
End If
Next
That should let you know if you are hitting you checkboxes. If not rexamine your page design.
I believe you should have no problem assigning ctrl to check, it should act as a pointer to ctrl. If you have more than one checkbox on the page, do an if statement against the ctrl.name to get the correct one.
I finally figured out what is going on.
I have the checkboxes inside different tables. These tables contains runat="server". This table is inside a Div tag that also contains a runat="server".
My code could never find the checkboxes because of this. I had to add a For Each that loops through the Div tag and find the appropriate table(s). I then had to loop through the tables in order to find the checkboxes.
Some of your controls have controls. Your loop will ignore those controls. I have some extension methods I use to get all controls (you can specify type CheckBox so you don't need to do the type check in your calling code)
<Extension()> _
Public Function ChildControls(ByVal parent As Control) As List(Of Control)
Return ChildControls(Of Control)(parent)
End Function
<Extension()> _
Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
Dim result As New ArrayList()
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is T Then result.Add(ctrl)
result.AddRange(ChildControls(Of T)(ctrl))
Next
Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
End Function
Well, I solved it as follows. (c#)
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc.ClientID == "menupadraolateral1")
{
foreach (Control cMnuLat in childc.Controls)
{
//here you can access the controls of usercontrol
}
}
}
}
Where "menupadraolateral1" is the ID used where the usercontrol is called
I hope I have helped

how to put focus on a dynamically generated field in update panel

i'm dynamically generating asp controls inside an update panel on a certain trigger. However, when the text control is generated, the focus goes on to the next pre-existing element and screws things up. Is there a way I can trigger a javascript event after the update panel has been updated?
The following worked when the control wasn't added though a async postback.
In the aspx/ascx control.
<asp:PlaceHolder runat="server" ID="placeHolder1"></asp:PlaceHolder>
In the codebehind.
TextBox test = new TextBox();
test.ID = "test";
test.TabIndex = 21;
test.Text = "test";
placeHolder1.Controls.Add(test);
string script = "document.getElementById('" + test.ClientID + "').focus();";
ScriptManager.RegisterStartupScript(this, typeof(TextBox), UniqueID, script, true);
It seems that the async postback sets the focus on the page. Pressing tab put the focus on the text Textbox.

How do I get dynamically added ASP.NET buttons to fire events?

This is probably a simple question but I am not an ASP.NET developer and I am quite stuck.
I have a simple search routine that returns between zero and several hundred results. Each of these must be added to the page as a button and I want to set the text of the button and the CommandArgument property so that when the button is clicked I can read the CommandArgument back and react accordingly.
However, when I click the button the event does not run at all. How can I get it to run?
The code for building the button list (simplified for readability) is as follows:
foreach (SearchResult sr in searchResults)
{
Button result = new Button();
result.Text = sr.name;
result.CommandArgument = sr.ID.ToString();
AccountSearchResults.Controls.Add(result);
result.Click += new EventHandler(SearchResultClicked);
AccountSearchResults.Controls.Add(new LiteralControl("<br/>"));
}
At the minute to test, I have popped a label on the form to put the CommandArgument in. This code is never executed though.
void SearchResultClicked(object sender, EventArgs e)
{
Label1.Text = ((Button)sender).CommandArgument;
}
You mentioned in another answer that you are adding these when a button is clicked. Looking at your code, I would suggest that you try setting a unique ID for each button added, then ensure that on loading the page that buttons with the same IDs and CommandArgument values are reloaded. When a dynamically loaded button is clicked, it must still exist on the page after postback for the event to fire.
I think the ID is all you need, plus your requirement for the CommandArgument). You could put the ID information in the ViewState if you can't get it repeat without a long search process.
Where are you adding this buttons?
if you are adding them inside another control then the event might be raising in the parent control. This happens on DataRepeaters and DataGrids for example.
I think you need to use the OnCommand event handler, rather than the OnClick i.e. try changing this:
result.Click += new EventHandler(SearchResultClicked);
to this:
result.Command += new EventHandler(SearchResultClicked);
UPDATE
Try changing the type of second argument to your event hander from EventArgs to CommandEventArgs. You might also have to set the CommandName property on your button i.e.
result.CommandName = "foo";

Resources