The code mentioned below is part of my webpart on sharepoint 2010.
public class class1 : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
string s1 = "first";
string s2 = "second";
string s3 = "third";
slist.Add("first");
slist.Add("second");
LinkButton b;
writer.Write("<div id='tblSegment' ><table >");
foreach (string s in slist)
{
//b = new LinkButton();
//b.Text = s;
//b.Click += (sender1, e1) => { b_Click(sender, e, s1, s2, s3); };
writer.Write("<tr><td >");
writer.Write("<a href='' >");
writer.Write(s + "</a>");
writer.Write("</td></tr>");
}
writer.Write("</table></div>");
}
void b_Click(object sender, EventArgs e, string s1, string s2, string s3)
{
UpdateList(s1,s2,s3);
}
public void UpdateList(string ID, string column, string value)
{
using (SPSite oSPsite = new SPSite("http://server"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
// Fetch the List
SPList list = oSPWeb.Lists["UserProfiles"];
//create Query
SPQuery query = new SPQuery();
query.Query = string.Concat(
"<Where><Eq>",
"<FieldRef Name='ID'/>",
"<Value Type='String'>" + ID + "</Value>",
"</Eq></Where>");
//get List Item
SPListItemCollection listItems = list.GetItems(query);
SPListItem item = listItems[0];
//update List Item
item[column] = value;
item.Update();
oSPWeb.AllowUnsafeUpdates = false;
}
}
}
}
I am kind of stuck here, what i need to do is to update a sharepoint list item based on some values(s1,s2,s3), for which i have created method UpdateList. I need to call UpdateList method whenever any hyperlink is clicked. I tried to use link buttons but as expected they did not worked(commented) with htmltextwriter, so i added two simple anchor tags. But how to fire the click event now.
I read about system.web.services but unable to make it work.
The only option i can think of now is to create a seprate web service with method UpdateList
in it kind of thing and make a ajax call to it.
Can any body suggest something else or is it somhow possible to use ASP.Net colrols(eg LinkButton) with Htmltextwriter
.