In ASP.NET, the TextBox text is not transferred back to the server when the TextBox is disabled. If you want the TextBox to post back the text, you can add a readonly attribute to the TextBox in the code behind.
protected void Page_Load(object sender, EventArgs e)
{
TB1.Attributes.Add("readonly", "readonly");
}
You could also add some style to the TextBox so it appears disabled:
.disabled
{
color:#ACA899;
}
<asp:textbox id="TB1" Runat="server" CssClass="disabled"></asp:textbox>
Wednesday, December 5, 2012
Wednesday, October 31, 2012
Add header variables to WCF service request using ChannelFactory
This is a way to add a specific soap header to a service request:
using (ChannelFactory factory = new ChannelFactory<ITest>(new BasicHttpBinding(BasicHttpSecurityMode.None), http://localhost/service.svc)){
using(ITest proxy = factory.CreateChannel()) {
//Sets up OperationContext so that the header can be added to soap request
using (OperationContextScope contextScope = new OperationContextScope(proxy)) {
var appHeader = new MessageHeader<string>("appID1");
string HeaderAppString = "application";
OperationContext.Current.OutgoingMessageHeaders.Add(appHeader.GetUntypedHeader (HeaderAppString, ns));
TestCandidateResponse retVal = proxy.Print(details);
}
}
}
This action will add an "application" node in the soap header packet with the value of appID1 as seen below:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<application xmlns="ns"> appID1 </application>
<s:Header>
<s:Body>
.........
</s:Body>
</s:Envelope>
Tuesday, October 2, 2012
Navigate back a page instead of the last PostBack
To go back a page in history instead of the last postback, this concept can be used.In the code behind
1.) Preferably, in a base page, create a counter:
private int postBackCount = 0;
2.) Create a HyperLink:
protected HyperLink previousPageLink;
3.) Override ViewState management and store the counter:
protected override void LoadViewState(object savedState)
{
object[] data = (object[])savedState;
base.LoadViewState(data[0]);
this.postBackCount = (int)data[1];
}
protected override object SaveViewState()
{
object [] data = new object[2];
data[0] = base.SaveViewState();
data[1] = this.postBackCount;
return data;
}
4.) In either Page_Load or OnPreRender, increment the counter appropriately and set the HyperLink value:
postBackCount = (IsPostBack) ? (postBackCount + 1) : 0;
previousPageLink.Attributes.Add("onclick", string.Format("Backward('{0}')", postBackCount.ToString()));
In the .aspx page
1.) Add the HyperLink
ID="previousPageLink" Runatimg src="/img/back.png" border="0" align="absmiddle">BackHyperLink>
2.) Add the javascript:
script type= text/javascript
function Backward(backCnt)
{
//Total number of post-backs
var refreshes = new Number(0);
//Allows the actual previous page to be selected
var offset = new Number(1);
refreshes = new Number(backCnt) + offset;
//Redirects to the actual previous page
history.go(-refreshes);
}
Thursday, August 30, 2012
Cannot evaluate expression because the code of the current method is optimized
To fix problems with debugging due to optimized code:1.) Pull up the project Properties under the "Project" menu
2.) Select the "Build" tab
3.) Uncheck "Optimize code" under the General area
Friday, August 10, 2012
Attach a client certificate to web service request
Attach an X509 client certificate to a web service request based on the certificates FriendlyName1.)
//using
using System.Security.Cryptography.X509Certificates;
2.)
//Create web service
WS_Service service = new WS_Service();
//Add certificate
service.ClientCertificates.Add(GetCertificate(certificateName));
service.ExecuteServiceRequest();
3.)
//Adding Certificate
private X509Certificate GetCertificate(string certName)
{
// Look in the local machine store.
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509CertificateCollection col = (X509CertificateCollection)store.Certificates;
X509Certificate cert = null;
for (int i = 0; i < col.Count; i++)
{
string friendlyName = ((X509Certificate2)col[i]).FriendlyName;
if (friendlyName.ToLower().Trim() == certName.ToLower().Trim())
{
cert = col[i];
break;
}
}
return cert;
}
Monday, July 23, 2012
SQL parse string
To parse a value from a column, PARSENAME and REPLACE can be used.
For instance, if the variable portion of a URL needs to be parsed, it can be done like this:
SELECT PARSENAME(REPLACE('http://www.theurl.com?var=1234', 'var=', '.'), 1) //Returns 1234
For instance, if the variable portion of a URL needs to be parsed, it can be done like this:
SELECT PARSENAME(REPLACE('http://www.theurl.com?var=1234', 'var=', '.'), 1) //Returns 1234
Thursday, June 28, 2012
C# deserialize JSON post
[Serializable]
public class Person
{
public int personId;
public int appId;
public string name;
}
string input = new StreamReader(Request.InputStream).ReadToEnd();
JavaScriptSerializer deserializer = new JavaScriptSerializer();
Person _person = deserializer.Deserialize
Console.Writeline(string.Format("Person delivered is: {0}", _person.name));
You can test this with the code below --------------------------------------------------:
string url = "the_test_url";
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
StringBuilder jsonData = new StringBuilder();
jsonData.AppendLine("{");
jsonData.AppendLine("\"personId\" : 1234,");
jsonData.AppendLine("\"appId\" : 2334,");
jsonData.AppendLine("\"name\" : \"John\"");
jsonData.Append("}");
byte[] dataArray = Encoding.UTF8.GetBytes(jsonData.ToString());
httpReq.Method = @"POST";
httpReq.ContentType = @"application/json";
httpReq.ContentLength = dataArray.Length;
httpReq.Timeout = 60000; // 1 minute (in milliseconds)
httpReq.KeepAlive = false;
using (Stream requestStream = httpReq.GetRequestStream())
{
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();
}
HttpWebResponse _resp = (HttpWebResponse)httpReq.GetResponse();
_resp.Close();
Subscribe to:
Posts (Atom)