在我的网站中,当我点击提交按钮时,它应该显示文本框中的所有链接,但是我的提交按钮不起作用,当我点击它时会发生这种情况。这是我的代码。我的提交按钮具有onclient点击属性。
protected void btnRender_Click(object sender, EventArgs e)
{
string strResult = string.Empty;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url.Text);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
strResult = strResult.Replace("<form id='form1' method='post' action=''>", "");
strResult = strResult.Replace("</form>", "");
//strResult = strResult.Replace("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><html xmlns="http://www.w3.org/1999/xhtml">");
div.InnerHtml = strResult;
}
protected void btn_createlink_Click(object sender, EventArgs e)
{
var links = TextBox1.Text.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var link in links)
{
if (!IsLinkWorking(link))
{
//Here you can show the error. You don't specify how you want to show it.
TextBox2.Text += string.Format("{0}\nNot working\n\n ", link);
}
else
{
TextBox2.Text += string.Format("{0}\n working\n\n", link);
}
}
}
bool IsLinkWorking(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//You can set some parameters in the "request" object...
request.AllowAutoRedirect = true;
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch
{
//TODO: Check for the right exception here
return false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
url.Text = "";
}
}
这是我的客户端代码 在这里,我有一个名为finda()的函数。当客户点击提交按钮时,它应该调用,但这不会发生
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function finda() {
var a = document.getElementsByTagName("a");
var b = document.getElementById("TextBox1");
b.value = "";
for (var i = 0; i < a.length; i++) {
a[i] = a.length.value;
if (a[i] == null) {
alert("Their is no links");
}
else {
b.value = b.value + "\r\n\n" + a[i];
}
}
// window.open("http://www.fillsim.com");
window.close();
// window.open("WebForm3.aspx?req=" + b.value);
}
</script>
<script type = "text/javascript">
var defaultText = "http://www.example.com";
function waterMarkText(txt, evt) {
if (txt.value.length == 0 && evt.type == "blur") {
txt.style.color = "red";
txt.value = defaultText;
}
if (txt.value == defaultText && evt.type == "focus") {
txt.style.color = "green";
txt.value = "";
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" >
Enter the URL:<br />
<br />
<asp:TextBox ID="url" runat="server" Width="263px"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnRender" runat="server" Text="Page Render" OnClick="btnRender_Click" />
<asp:Button ID="btn_submit" runat="server" Text="Submit" OnClientClick="javascript:finda();" />
<asp:Button ID="btn_createlink" runat="server"
Text="Create link" OnClick="btn_createlink_Click" />
<asp:TextBox ID="TextBox2" runat="server" Height="371px" TextMode="MultiLine" Width="409px"></asp:TextBox>
<div class="ab" id="div" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Clear"
Width="71px" />
</div>
</form>
答案 0 :(得分:0)
您的form
标记应具有runat="server"
属性,以便可以正确处理ASP.NET回发事件。