Sunday, March 11, 2012

Getting the value of DynamicContextKey

In case I wasn't clear enough, here is the full code sample:

<div id="divAddComment" class="ModalPopup" style="display: none;">
<div class="Control">
<asp:TextBox ID="TextBoxComment" runat="server" TextMode="multiLine" Rows="5" />
</div>
<asp:Button ID="ButtonAddComment" runat="server" Text="Add comment" OnClick="ButtonAddComment_Click" />
<asp:Button ID="ButtonCancel" runat="server" Text="Cancel" />
<asp:Label ID="LabelID" runat="server" />
</div
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
<ItemTemplate
<asp:LinkButton ID="LinkButtonAddComment" runat="server" Text="Add comment" />
<ajaxToolkit:ModalPopupExtenderID="ModalPopupExtenderAddComment"
runat="Server"
DynamicServiceMethod="GetContent"
DynamicContextKey='<%# Eval("ID")%>'
DynamicControlID="LabelID"
BackgroundCssClass="modalBackground"
TargetControlID="LinkButtonAddComment"
PopupControlID="divAddComment" /
</ItemTemplate>
</asp:Repeater>

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string GetContent(string contextKey)
{
return contextKey;

// I thought about saving the value here, but I think this needs to be
// a static method to work
}

protected void ButtonAddComment_Click(object sender, EventArgs e)
{
// when the modal pop is displayed, the correct value is displayed for LabelID

// when I try to access the LabelID.Text property,
// the value is always an empty string

// how can I access this value from this function?

}

If anyone has any ideas, please let me know. This one has me stumped.


You can get and set the DynamicContextKey using the private _dynamicContextKey property

where "popone" is the id or bahaviourID for you modal modalpopup extender.

popper = $find("popone");
if (popper){
popper._DynamicContextKey = ctxval;
popper.show();
}

If you want to use this client side you could drop it into hiddenfield value or use a PageMethod.


Thanks for the reply. Is there a way I can get the DynamicContextKey value from my ButtonAddComment_Click() event? I can't figure out how to access the ModalPopupExtender because it is inside a Repeater.


If you put the value there server side then you know what the value is, so reference the

value in the normal way. If you have changed the value on the client side, you can get get it back server side by putting it into the value of hidden field.

So

in the clientside onclick event

1. $get(hiddenFldId).value = $find('poper')._DynamicContextKey

2. return true;

On serverside

hiddenfld.value will contain client side value.

If you want to get the correct hiddenFldId I use <% =hiddenFld.ClientID %>

Does this help?


I'm trying to access the value server side. This is where I'm having the problem. The value for theLabelID displays the DynamicContextKey value OK in the modal popup, but when I access the LabelID.Text property in code it is always an empty string.

I also tried using a HiddenField control instead, but the same thing happens with the HiddenField.Value property.


Hi,

The content of a label won't be posted to server, so you need to use a HiddenField or TextBox to save it on client.

Here is a sample:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string GetContent(string contextKey)
{
return contextKey;

// I thought about saving the value here, but I think this needs to be
// a static method to work
}

protected void ButtonAddComment_Click(object sender, EventArgs e)
{
// when the modal pop is displayed, the correct value is displayed for LabelID

// when I try to access the LabelID.Text property,
// the value is always an empty string

// how can I access this value from this function?
Response.Write(TextBox1.Text);
}

protected void Page_Load(object sender, EventArgs e)
{
Northwind nt = new Northwind();
System.Data.DataTable dt = nt.GetBasicInformationOfAllEmployees();
Repeater1.DataSource = dt;
Repeater1.DataBind();
}

protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{

}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager
</div
<div id="divAddComment" class="ModalPopup" style="display: none;">
<div class="Control">
<asp:TextBox ID="TextBoxComment" runat="server" TextMode="multiLine" Rows="5" />
</div>
<asp:Button ID="ButtonAddComment" runat="server" OnClientClick="saveValue();"Text="Add comment" OnClick="ButtonAddComment_Click" />
<asp:Button ID="ButtonCancel" runat="server" Text="Cancel" />
<asp:Label ID="LabelID" runat="server" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div
<script type="text/javascript">
function saveValue()
{
var tb = $get("TextBox1");//.value = innerHTML
var lb = $get("LabelID");//.value;
tb.value = lb.innerHTML;
}
</script>

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
<ItemTemplate
<asp:LinkButton ID="LinkButtonAddComment" runat="server" Text="Add comment" />
<ajaxToolkit:ModalPopupExtenderID="ModalPopupExtenderAddComment"
runat="Server"
DynamicServiceMethod="GetContent"
DynamicContextKey='<%# Eval("EmployeeID")%>'
DynamicControlID="LabelID"
BackgroundCssClass="modalBackground"
TargetControlID="LinkButtonAddComment"
PopupControlID="divAddComment" /
</ItemTemplate>
</asp:Repeater
</form>
</body>
</html>

Hope this helps.

Thanks! That was exactly what I was looking for, it's working great.

No comments:

Post a Comment