在.NET中,默认情况下,服务器端控件的客户端ID与生成的文本连接。
例如:
<asp:TextBox ID="txtUser" runat="server">
会变成......
<input type="text" id="ctl00_body_txbUser">
但是当我使用 HttpWebRequest.GetResponse(objReq.Getresponse,HttpWebResponse)来请求同一页面时,该项目会返回而不会生成自动生成的文本。
<input type="text" id="txbUser">
是否可以使用HttpWebRequest对象和GetResponse,以便它返回一个响应,其中自动生成ID的.NET用于服务器端控件?
我正在与之前已设置特定于ID的转换规则的第三方合作,现在我们尝试使用相同的规则来处理通过从页面生成的字符串的API调用。但是,从页面生成的字符串不具有相同的ID。
下面是用于将网页作为字符串返回的代码。
Public Shared Function GetWebPageAsString(ByVal strURI As String, ByVal strPostData As String) As String
' Declare our variables. '
Dim objHttpRequest As HttpWebRequest
Dim PostBuffer() As Byte
Dim PostDataStream As Stream = Nothing
Dim objHttpResponse As HttpWebResponse = Nothing
Dim objStreamReader As StreamReader = Nothing
Dim strResponseText As String = ""
Try
' Create a new request. '
objHttpRequest = CType(WebRequest.Create(strURI), HttpWebRequest)
objHttpRequest.Timeout = 3000000
objHttpRequest.Method = "POST"
PostBuffer = Encoding.ASCII.GetBytes(strPostData)
objHttpRequest.ContentType = "application/x-www-form-urlencoded"
objHttpRequest.ContentLength = PostBuffer.Length
PostDataStream = objHttpRequest.GetRequestStream
PostDataStream.Write(PostBuffer, 0, PostBuffer.Length)
PostDataStream.Close()
' Get the response to our request as a stream object. '
objHttpResponse = CType(objHttpRequest.GetResponse, HttpWebResponse)
' Create a stream reader to read the data from the stream. '
objStreamReader = New StreamReader(objHttpResponse.GetResponseStream, Encoding.UTF8)
' Copy the text retrieved from the stream to a variable. '
strResponseText = objStreamReader.ReadToEnd()
' Close our objects. '
objStreamReader.Close()
objHttpResponse.Close()
Catch ex As Exception
strResponseText = strURI & " | " & strPostData
Throw (ex)
Finally
If Not objStreamReader Is Nothing Then
objStreamReader.Close()
End If
If Not PostDataStream Is Nothing Then
PostDataStream.Close()
End If
If Not objHttpResponse Is Nothing Then
objHttpResponse.Close()
End If
objHttpRequest = Nothing
PostBuffer = Nothing
PostDataStream = Nothing
objHttpResponse = Nothing
objStreamReader = Nothing
End Try
' Set return value. '
Return strResponseText
End Function
编辑: 为了澄清,我需要继续由.NET自动生成ID。我知道我可以通过将模式设置为静态来使它们相等。不幸的是,我们正在使用的第三方已经根据.NET生成的ID为我们当前的页面创建了规则。使用HTTPRequest对象请求同一页面并将数据推送到流中。我不再看到自动生成的ID,即使它是同一页 。
答案 0 :(得分:1)
创建一个干净的母版页并将您的页面放入其中。这应该可以解决ID问题。