我正在尝试更改UpdatePanel中的div内容。
我写的代码成功更改了div,但只在第二个按钮点击,而不是在第一个按钮上。
就像UpdatePanel.Update()一样,它没有刷新html。
请注意,所有内容(按钮和div)都在模态内。
这里是html部分:
<div class="modal-body">
<form name="frmInsOpe">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:Button ID="btnInsOpe" Text="Button" OnClick="btnInsOpe_Click" runat="server" />
</form>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="frameModal" runat="server"></div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnInsOpe"/>
</Triggers>
</asp:UpdatePanel>
</div>
和.cs
protected void Page_Load(object sender, EventArgs e)
{
}
private void Reload(string url)
{
string responseFromServer = String.Empty;
try
{
string strBuffer = "";
byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);
//Initialization
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(Answer);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
Answer.Close();
WebResp.Close();
}
catch(Exception ex)
{
responseFromServer = String.Format("<div><input type='text' value='Errore durante la richiesta: {0}' /></div>", ex.Message);
}
// Display the content.
frameModal.Attributes["innerHtml"] = responseFromServer;
UpdatePanel1.Update();
}
protected void btnInsOpe_Click(object sender, EventArgs e)
{
Reload(ConfigurationManager.AppSettings["myUrl"]);
}
有什么想法吗?