custom data-attribute in asp.net Linkbutton - c#

can i use custom attributes with linkbutton?
like this:
var linkb= new LinkButton();
linkb.ID = "myLinkButton";
linkb.Text = "x";
linkb.Click += linkbuttonClicked;
linkb.Attributes["data-id"] = "DataId";
linkb.CssClass = "lb";
MyPanel.Controls.Add(linkb);
Can i do something like this onClick?
var thisButton= (LinkButton) sender;
var test = thisButton.Attributes["data-id"]);

As far as I know, you can do that in ASP.NET 4.0, the attribute and value will be saved in the viewstate and persist across postbacks.
But, as it persists only in the viewstate, if you change its value client-side with some js/jquery, the new value will not be sent to the server on postback and will not persist.

Related

c# How to utilize Page_init, ViewState and Page_Load properly?

I have been working with Web Forms for a short while now and I have read most of what I have found about this on google. However, I am still unclear on how to work with this properly. I like the picture in this answer, but find it a bit too generic. I have not found one decent, concrete example on how to work with these events.
I am currently creating several controls dynamically in code behind in the Page_Load event:
foreach (Service service in Services)
{
// service div
HtmlGenericControl serviceDiv = new HtmlGenericControl("div");
serviceDiv.ID = service.ID;
serviceDiv.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
outerDiv.Controls.Add(serviceDiv); //outerDiv exists in the aspx page
// service updatepanel
UpdatePanel uPanel = new UpdatePanel()
{
ID = service.ID + "_uPanel",
UpdateMode = UpdatePanelUpdateMode.Conditional
};
serviceDiv.Controls.Add(uPanel);
// status span
HtmlGenericControl statusSpan = new HtmlGenericControl("span");
statusSpan.ID = service.ID + "_statusSpan";
statusSpan.InnerHtml = service.Status;
uPanel.ContentTemplateContainer.Controls.Add(statusSpan);
// show specific content
if (service.Status.Equals(ServiceControllerStatus.Running.ToString()))
{
// status color
statusSpan.Attributes.Add("class", "status-run");
// stop button
HtmlButton stopButton = new HtmlButton();
stopButton.ID = service.ID + "_btnStop";
stopButton.InnerHtml = "<i class=\"fa fa-stop btn-red\"/></i>";
stopButton.Attributes.Add("type", "button");
stopButton.Attributes.Add("runat", "server");
stopButton.Attributes.Add("class", "btn btn-link btn-xs");
stopButton.Attributes.Add("title", "Stop");
stopButton.ServerClick += new EventHandler(BtnStop_Click);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(stopButton);
uPanel.ContentTemplateContainer.Controls.Add(stopButton);
// restart button
HtmlButton restartButton = new HtmlButton();
restartButton.ID = service.ID + "_btnRestart";
restartButton.InnerHtml = "<i class=\"fa fa-refresh btn-blue\"/></i>";
restartButton.Attributes.Add("type", "button");
restartButton.Attributes.Add("runat", "server");
restartButton.Attributes.Add("class", "btn btn-link btn-xs");
restartButton.Attributes.Add("title", "Restart");
restartButton.ServerClick += new EventHandler(BtnRestart_Click);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(restartButton);
uPanel.ContentTemplateContainer.Controls.Add(restartButton);
}
else
{
// status color
statusSpan.Attributes.Add("class", "status-stop");
// start button
HtmlButton startButton = new HtmlButton();
startButton.ID = service.ID + "_btnStart";
startButton.InnerHtml = "<i class=\"fa fa-play btn-green\"/></i>";
startButton.Attributes.Add("type", "button");
startButton.Attributes.Add("runat", "server");
startButton.Attributes.Add("class", "btn btn-link btn-xs");
startButton.Attributes.Add("title", "Start");
startButton.ServerClick += new EventHandler(BtnStart_Click);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(startButton);
uPanel.ContentTemplateContainer.Controls.Add(startButton);
}
// version span
HtmlGenericControl versionSpan = new HtmlGenericControl("span");
versionSpan.ID = service.ID + "_version";
versionSpan.InnerHtml = service.Version;
versionSpan.Attributes.Add("class", "version-text");
serviceDiv.Controls.Add(versionSpan);
What would I gain from creating these in Page_Init? If I create them in Page_Init, how do I access them in Page_Load? Private global lists of UpdatePanels and HtmlButtons feels so unclean.
I know that the ViewState loads between Page_Init and Page_Load, but what does that really mean? Since I don't do full postbacks, but instead use RegisterAsyncPostBackControl to only update the UpdatePanel's on postback, don't I need to re-populate in Page_Load?
If you don't need the ViewState, then you can also create the controls in Page_Load. The ViewState is used to store the values of the controls (which ones depends on the control) and to use them when the PostBack is sent to the server.
For instance, if you have a textbox, the PostBack contains the new value of the textbox and also the old value in the ViewState. The ASP.NET framework now compares those two and raises the TextChanged event if necessary. Without ViewState, this would not be possible.
The best advice you can give regarding dynamically created controls in ASP.NET WebForms is to avoid using them. They increase complexity very fast und are usually not necessary (even if it seems so at first).
In the majority of the cases, there is a much simpler approach, e.g. by using a Repeater. In your case, you have a list of services. You can bind this list to a Repeater and by that avoid to create the controls manually. See this link on how to use a Repeater.
Another upside of using a Repeater is that you can define the UI in the ASPX markup instead of in the code behind file. In your example, you change the UI based upon the status of the service. In this case, using the ItemDataBound-event of the Repeater might be a good option.

Assigning textbox values to session

I cant understand whats wrong with this simple code.
Page load starts with filling textboxes with session data,
tbgiris.Text = Convert.ToString(Session["giris"]);
tbmail.Text = Convert.ToString(Session["mail"]);
tbisim.Text = Convert.ToString(Session["isim"]);
tbgrup.Text = Convert.ToString(Session["grup"]);
tbkimlik.Text = Convert.ToString(Session["kimlik"]);
tbadmin.Text = Convert.ToString(Session["admin"]);
This works fine. Then, I edit those textbox values manually in browser, and call the button function which must simply update the session values with given data in textboxes, and re-write textboxes as a confirmation,
Session["giris"] = tbgiris.Text;
Session["mail"] = tbmail.Text;
Session["isim"] = tbisim.Text;
Session["grup"] = tbgrup.Text;
Session["kimlik"] = tbkimlik.Text;
Session["admin"] = tbadmin.Text;
tbgiris.Text = Convert.ToString(Session["giris"]);
tbmail.Text = Convert.ToString(Session["mail"]);
tbisim.Text = Convert.ToString(Session["isim"]);
tbgrup.Text = Convert.ToString(Session["grup"]);
tbkimlik.Text = Convert.ToString(Session["kimlik"]);
tbadmin.Text = Convert.ToString(Session["admin"]);
This is where I fail. Session values stay same, does not get updated. If I change the session value to a static value in button function, like,
Session["giris"] = "test string";
it works fine, updates the session and textbox. But if I try to get values from textboxes, code behaves like I have never changed texts of textboxes.
Thanks in advance
code behaves like I have never changed texts of textboxes
You did, but then you changed it back.
In the ASP.NET WebForms page lifecycle, Page_Load is executed on a post-back before event handlers are executed. So what's happening is:
Set the TextBox value in Page_Load
Display the page
Change the value
Submit the page
Set the TextBox value in Page_Load (this is the problem)
Get the TextBox value in event handler
In order to prevent your Page_Load logic from executing on a post-back, you'd wrap it in a conditional:
if (!IsPostBack)
{
// your logic here
}
That way the code will execute only when the page is first loaded, so you're not over-writing the updated values on every post-back.
put this inside because when page post back it assign the same old value in the session to textboxes thats why you are getting the old values.
if (!IsPostBack)
{
tbgiris.Text = Convert.ToString(Session["giris"]);
tbmail.Text = Convert.ToString(Session["mail"]);
tbisim.Text = Convert.ToString(Session["isim"]);
tbgrup.Text = Convert.ToString(Session["grup"]);
tbkimlik.Text = Convert.ToString(Session["kimlik"]);
tbadmin.Text = Convert.ToString(Session["admin"]);
}

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.

RequiredFieldValidator not working in a repeater

I'm trying to dynamically add a RequiredFieldValidator to a RadioButtonList in a repeater, but it fails with the error:
Unable to find control id 'rblAccessory_40' referenced by the 'ControlToValidate' property of ''.
The code for this section is:
if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
{
Label lblAccID = (Label)e.Item.FindControl("lblAccID");
RadioButtonList rblCondition = (RadioButtonList)e.Item.FindControl("rblCondition");
rblCondition.ID = "rblAccessory_" + lblAccID.Text;
if (conditionList.Count() > 0)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
rfv.ErrorMessage = "Please complete the accessories section";
pnlValidation.Controls.Add(rfv);
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
}
foreach (ListItem li in rblCondition.Items)
{
li.Text = "";
li.Value = "AccessoryID_" + lblAccID.Text + "-ConditionID_" + li.Value;
}
}
}
It is definitely finding the RadioButtonList (rblCondition) because the data is binding correctly at this point:
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
So I don't understand why the error says it is unable to find the control ID.
I've tried specifying the control ID manually, as below:
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
and have also tried:
rfv.ControlToValidate = rblCondition.ID;
lblAccID is a hidden text field used to store the ID of the row in the repeater.
Assign the control to validate property AFTER the ItemCreated event. I know this sounds weird, how would you still have the reference to the dynamically created control? I've gotten around this by keeping a reference to a List<Action> reference which I add things to during ItemCreated to be executed later.
In your control class you'll declare your List<Action> object:
List<Action> deferringControlToValidateUntilPreRender = new List<Action>();
Inside the ItemCreated event you'll have a line that looks like:
deferringControlToValidateUntilPreRender.Add(() => rfv.ControlToValidate = rblCondition.UniqueID);
And then, later, perhaps in PreRender:
foreach(var deferredAction in deferringControlToValidateUntilPreRender) action();
Since the RequiredFieldValidator is client-side code, you need to use the client id of the control. Like this:
rfv.ControlToValidate = rblCondition.ClientID;
Some more information from MSDN if you are interested:
Control.ClientIDMode Property
Also, an SO discussion on the differences between ClientID and UniqueID (as referenced in #MushinNoShin's answer, which, IMHO, is incorrect):
Why is there a difference between ClientID and UniqueID?

Load a silverlight to aspx page through code behind

I need to load a silverlight application in a portion of an aspx page on a button click on that page page. Some init parameters need to be passed to the silverlight application based on the user inputs on the host page on button click. How to do that?
I presume I need to create the silverlight object from code-behind to set custom InitParameters. Any idea how to do that?
Extending to what is mentioned here, you can do something like this:
HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
this.Page.Controls.Add(myHtmlObject);
HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApplication.xap";
myHtmlObject.Controls.Add(mySourceParam);
HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "onError";
myOnErrorParam .Attributes["value"] = "onSilverlightError";
myHtmlObject.Controls.Add(myOnErrorParam);
HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "InitParameters";
myOnErrorParam .Attributes["value"] = "param1=Hello,param2=World";
myHtmlObject.Controls.Add(myInputParam);
this.Page.Controls.Add(myHtmlObject);

Categories

Resources