How to set dynamic parameter values to custom control from web page - c#

I have one image button in the custom control like below.
public string SearchTableName = string.Empty;
public string SearchColumnName = string.Empty;
public string SiteURL = string.Empty;
ImageButton _imgbtn;
protected override void OnInit(EventArgs e)
{
_imgbtn = new ImageButton();
_imgbtn.ImageUrl = ImageURL;
_imgbtn.OnClientClick = "ShowSearchBox('" + SiteURL +"/_layouts/CustomSearch/SearchPage/Searchpage.aspx?table_name=" + SearchTableName + " &column_name=" + SearchColumnName + "')";
}
On Clicking of the image button I want to migrate to the another window which is a popup. For this I written a javascript function. I am setting the SearchTableName and SearchColumnName in the web page in which we are consuming this custom control like below. Before consuming I registered this control in web page with register tag.
<ncc:SearchControl runat="server" ID="txtSearchControl" /> In code behind file of this webpage I am using following code to set the values.
protected void Page_Load(object sender, EventArgs e)
{
txtSearchControl.ImageURL = "_layouts/Images/settingsicon.gif";
txtSearchControl.SearchTableName = "Employees";
txtSearchControl.SearchColumnName = "LastName";
txtSearchControl.SiteURL = "http://Sp2010:8787";
}
Now coming to the problem, when I click the image button the SearchTableName and SearchColumnName values are not coming. I think I am calling OnClientClick function, thats why the values are not being set. But how to set the values for the custom control based on the values setting in the webpage. If I use the Click function will it serve my purpose? If so, how to call that javascript function from this click event.

Finally got solution. I am initializing the values in the page init method in the custom control. Thats why the values i am setting in the visual webpart page are not being captured. Now I changed the initializing the values in CreateChildControl method. Now it works perfectly. Thank you.

Related

Alternative to Document.GetElementFromPoint(e.ClientMousePosition) in webview in c# win forms

I am using
HtmlElement elem = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
to get element from point in web browser control. I want to get the html element on mouse click in web view.Any alternative to this in web view control
As per the explanation here:
Web view
Interacting with web view content
You can interact with the content of the web view by using the
InvokeScriptAsync method to invoke or inject script into the web view
content, and the ScriptNotify event to get information back from the
web view content.
So you could do something like this:
private void WebView1_DOMContentLoaded(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlDOMContentLoadedEventArgs e)
{
String func = #" document.addEventListener('click', function(e){
//Get the Div Tag Name and its ID as string back into our code
window.external.notify(e.target.tagName + '(' + e.target.id + ')');
});";
webView1.InvokeScriptAsync("eval", func);
}
private void WebView1_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e)
{
string s = e.Value; //This will give you the Div Tag Name and ID
}

Asp.net Custom Table Cell ClientID not set

I have a class derived from WebControls.TableCell.
When the Text property is set, I call a method that dynamically adds asp:Panels and asp:LiteralControls to the Cell. I want to reference these controls in Javascript, so naturally I tried using the ClientId of the panels in my JS functions. However, these controls have no ClientId set (the string is empty). Why is this? How do I force the ClientIds to be set?
As a temporary solution, I set the ClientIDMode to "static" and created the IDs on my own, but this is not satisfactory because it's hard to reference those IDs in JS. Why? If you assign, for example, "12345" to one control, it gets changed on client side to something like "MainContent_123456". This is bad because the "MainContent" part is not fixed; thus I never know for sure what the real Id on the client side will be. Currently, I can get the control with jQuery using $ctrl = $('[id$='12345']');, but this is dirty because it would get any control that has '123456' in its id.
So, back to the original question: how do I get my ClientIds set automatically for my panels in my custom TableCells?
Edit: Code added
protected void Page_Load(object sender, EventArgs e)
{
this.ClientIDMode = System.Web.UI.ClientIDMode.Static;
}
Code in the method that adds the controls to the custom TableCell:
Panel remainingTextPanel = new Panel();
remainingTextPanel.ID = Guid.NewGuid().ToString();
remainingTextPanel.Style["display"] = "none";
LiteralControl remainingText = new LiteralControl(myText.Substring(initialStringLength, myText.Length - initialStringLength));
remainingTextPanel.Controls.Add(remainingText);
this.Controls.Add(remainingTextPanel);
Panel linkBtnPanel = new Panel();
LinkButton lnkBtn = new LinkButton() {Text = "...", OnClientClick = "toggleDynamicText('" + remainingTextPanel.ID + "'); return false;" };
lnkBtn.Font.Bold = true;
linkBtnPanel.Controls.Add(lnkBtn);
this.Controls.Add(linkBtnPanel);
And the JS Code:
function toggleDynamicText(id) {
$ctrl = $('[id$=' + id + ']');
$(document).ready(function () {
$ctrl.toggle(1000);
});
}
Without seeing any code it's difficult to say what's going on but to access your controls using jQuery you can do the following:
$("#<%=myElement.ClientID%>")
This way it doesn't matter what .NET assigns as the ID.

ImageButton ASP.Net changing Image URL in a control

I'm setting up a Web User Control in ASP 4. The control itself works correctly, and the code for the onload even is the same as used in my standard aspx pages.
protected void GetTranslationImage(object sender, EventArgs e)
{
ImageButton image = (ImageButton)sender;
objTranslation = new TranslationsHelper();
string sTranslationID = image.ImageUrl.ToString();
string lang = ((NRMaster)this.Master).Language;
lang = lang == null ? "en-gb" : lang;
image.ImageUrl = objTranslation.GetTranslation(sTranslationID, lang);
}
When the object is passed through onLoad it doesn't contain the default URL during GetTranslationImage. The object changes to the translation required however it reverts back following in the HTML on the page load.
The definition of the ImageButton is as follows:
<asp:ImageButton ID="btnSearch" runat="server" name="Search" value="Search" class="Search" src="/_resources/img/BTN_search.gif" onclick="btnSearch_Click" onLoad="GetTranslationImage" />
Does the control alter the time this function needs to be run.
Any help would be greatly appreciated.
Thank You
James
Why are you setting src="/_resources/img/BTN_search.gif" if you are overwriting it in OnLoad?
1: Remove it
Or
2: Change it to:
ImageUrl="/_resources/img/BTN_search.gif"
What if you made the transition method public from within the usercontrol and you simply call out to that method in your page load?
page_load()
{
myusercontrol.StartImageTransition();
}
Public Methods inside User control c# .net

Call javascript function in server side

How to call a javascript function inside server side after the code is executed in page load/any events ? I am using UpdatePanel in this page. I had tried Page.RegisterStartUpScript, ClientScript.RegisterStartupScript. None of this works.
With an UpdatePanel you need to use ScriptManager.RegisterStartupScript, like this:
var script = "alert('hi);";
ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true);
You have to remember in an UpdatePanel, you're not sending the whole page back to the client, so the Page versions won't work, because their content never goes anywhere on a partial update. With ScriptManager it actively shoves this into the AJAX response it's sending as a script to execute, so it's behaving a little differently.
Just yesterday I did some research to help a fellow co-worker out and came up with the following solution. It relys on some techniques used in ajax control extenders in the use of registering data items. Since I wanted this to be more of a generic approach, I placed the following code in a script block in the master page just after the scriptmanager object:
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
function PageLoadingHandler(sender, args) {
var dataItems = args.get_dataItems();
if ($get('<%=JSBridge.ClientID%>') !== null) {
eval(dataItems['<%=JSBridge.ClientID%>']);
}
}
Then somewhere in the markup of the master page I placed a hiddenfield as in:
asp:HiddenField ID="JSBridge" runat="server"
That's it for the master page. Now, all of my webpages inherit from a base page so I placed a method in the base page as in:
public void InvokeScriptMethod(string methodName, string[] methodArgs)
{
string sArgs = string.Empty;
string delim = string.Empty;
bool isNumeric = false;
int iArg = 0;
if (methodArgs != null && methodArgs.Length > 0)
{
foreach (string arg in methodArgs)
{
isNumeric = int.TryParse(arg, out iArg);
sArgs += delim + ((isNumeric) ? arg : "'" + arg + "'");
delim = ",";
}
}
ScriptManager manager = (ScriptManager)Master.FindControl("ScriptManager1");
if (manager.IsInAsyncPostBack)
{
manager.RegisterDataItem(Master.FindControl("JSBridge"), methodName + "(" + sArgs + ")");
}
}
So, assuming your content is inside an update panel, any button clicks or any event for that matter, on any web page, you can simply do the following:
protected void MyButton_Click(object sender, EventArgs e)
{
//-- Call base page method to invoke javascript call
InvokeScriptMethod("ShowMessage", new string[] { "David", "Whitten", "44" });
}
This is assuming you have a javascript method out there somewhere called "ShowMessage" with the necessary parameters. Obviously, one can specify any method name and any numbers of parameters.
Just wanted to share my findings. Maybe there is even a better way but I find this pretty simple and flexible.
David
ScriptManager.RegisterStartupScript
Registers a startup script block for a
control that is inside an UpdatePanel
by using the ScriptManager control,
and adds the script block to the page.

Ajax ReordeList and asp:FileUpload Problem

I have a ReorderList which is working fine, inside the InsertItemTemplate, I've added a asp:Fileupload to add images to the list and database. All of these controls are inside a DIV.
How could I reach to this (asp:FileUpload) in C# to check whether it has a file or not,
this is the C# part of the code:
///////////////////////////////////////////////////////////////////////////////////////////////
protected void btnInsert_Click(object sender, EventArgs e)
{
string sFilename = Guid.NewGuid().ToString();
FileUpload filePhoto = (FileUpload)div1.FindControl("filePhoto");
if (filePhoto.HasFile)
{
string sPath = "";
string sFile = filePhoto.FileName.ToString();
sPath = Server.MapPath("Images");
filePhoto.SaveAs(sPath + "\\" + sFile);
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + sFile;
}
else
{
//to fill the Notice image by code behine
ObjectDataSource1.InsertParameters["theImage"].DefaultValue = "Images\\" + "NoImage.jpg";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
any ideas?
Thanks in advance
Actually ReorderList is an ajax control and you cannot use normal Asp:Fileuploader in ajax control. You have to use the asyncfileuploader control of ajax control toolkit in order to work in ajax application.

Categories

Resources