Asp.net WeChat Development (User Group Management), asp.net Group

Source: Internet
Author: User

Asp.net Development (User Group Management), asp.net Group

The previous article explains how to create a user group and move users to a group. This chapter describes how to modify the group name and delete a group.

Directly code the object class that is required to obtain the group list.

/// <Summary> /// group class /// </summary> public class WxGroupsInfo {public string Group_ID {get; set ;} // Group No. public string Group_Name {get; set;} // group name public string Group_Count {get; set;} // group count}

Front-end code:

<! DOCTYPE html> 

The background code is as follows:
 

Protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {BindGroupList (); this. dataBind () ;}} private void BindGroupList () {WeiXinServer wxs = new WeiXinServer (); // read accesstoken string Access_token = Cache ["Access_token"] as string; if (Access_token = null) {// if it is null, obtain Access_token = wxs again. getAccessToken (); // set the cached data to expire after 7000 seconds. insert ("Access_token", Access_token, null, DateTime. now. addSeconds (7000), System. web. caching. cache. noSlidingExpiration);} string Access_tokento = Access_token.Substring (17, Access_token.Length-37); string jsonres = ""; string content = Cache ["AllGroups_content"] as string; if (content = null) {jsonres =" https://api.weixin.qq.com/cgi-bin/groups/get?access_token= "+ Access_tokento; HttpWebRequest myRequest = (HttpWebRequest) WebRequest. create (jsonres); myRequest. method = "GET"; HttpWebResponse myResponse = (HttpWebResponse) myRequest. getResponse (); StreamReader reader = new StreamReader (myResponse. getResponseStream (), Encoding. UTF8); content = reader. readToEnd (); reader. close (); // set the Cache data to expire after 7000 seconds. insert ("AllGroups_content", content, null, DateTime. now. AddSeconds (7000), System. web. caching. cache. noSlidingExpiration);} // Newtonsoft must be referenced before use. json. dll file JObject jsonObj = JObject. parse (content); int groupsnum = jsonObj ["groups"]. count (); List <WxGroupsInfo> wxgrouplist = new List <WxGroupsInfo> (); for (int I = 0; I <groupsnum; I ++) {WxGroupsInfo wginfo = new WxGroupsInfo (); wginfo. group_ID = jsonObj ["groups"] [I] ["id"]. toString (); wginfo. group_Name = j SonObj ["groups"] [I] ["name"]. toString (); wginfo. group_Count = jsonObj ["groups"] [I] ["count"]. toString (); wxgrouplist. add (wginfo);} this. repeaterGroupList. dataSource = wxgrouplist; this. repeaterGroupList. dataBind ();} /// <summary> /// bind the event /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void RepeaterGroupList_ItemDataBound (object sender, repeaterItemEventArgs E) {if (e. item. itemType = ListItemType. item | e. item. itemType = ListItemType. alternatingItem) {Label lbXuHao = e. item. findControl ("lbXuHao") as Label; int num = 1; lbXuHao. text = num. toString (); for (int I = 0; I <this. repeaterGroupList. items. count; I ++) {num + = 1; lbXuHao. text = num. toString ();} LinkButton LinkBtnDeleteGroup = e. item. findControl ("LinkBtnDeleteGroup") as LinkButton; LinkBtnDelet EGroup. Attributes. Add ("OnClick", "return confirm ('Are you sure you want to delete this group? After deletion, the people in the group will be restored to the default group! ')");}} /// <Summary> /// execution event /// </summary> /// <param name = "source"> </param> /// <param name = "e"> </param> protected void RepeaterGroupList_ItemCommand (object source, repeaterCommandEventArgs e) {// ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('deletion interface closed! '); Location = 'wxgroupmanagelist. aspx '; ", true); if (e. commandName = "DeleteGroups") {WeiXinServer wxs = new WeiXinServer (); string res = ""; /// read accesstoken string Access_token = Cache ["Access_token"] as string; if (Access_token = null) {// if it is null, obtain Access_token again = wxs. getAccessToken (); // set the cached data to expire after 7000 seconds. insert ("Access_token", Access_token, null, DateTime. now. addSeconds (7000), System. web. caching. cache. noSlidingExpiration);} string Access_tokento = Access_token.Substring (17, Access_token.Length-37); string posturl =" https://api.weixin.qq.com/cgi-bin/groups/delete?access_token= "+ Access_tokento; // POST Data example: {" group ": {" id ": 108} string groupid = e. commandArgument. toString (); string postData = "{\" group \ ": {\" id \ ": \" "+ groupid. toString () + "\"} "; res = wxs. getPage (posturl, postData); ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('deleted successfully! Due to cache problems, you may need to log on again to see the effect! '); Location = 'wxgroupmanagelist. aspx '; ", true );}} /// <summary> /// create a group /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void LinkBtnCreateGroup_Click (object sender, eventArgs e) {if (this.txt groupsName. value. toString (). equals ("group name") {// ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('cannot be blank! ') ", True); this.txt groupsName. focus (); return;} WeiXinServer wxs = new WeiXinServer (); string res = ""; // read accesstoken string Access_token = Cache ["Access_token"] as string; if (Access_token = null) {// if it is null, obtain Access_token = wxs again. getAccessToken (); // set the cached data to expire after 7000 seconds. insert ("Access_token", Access_token, null, DateTime. now. addSeconds (7000), System. web. caching. cache. noSlidingExpiration);} string Access_tokento = Access_token.Substring (17, Access_token.Length-37); string posturl =" https://api.weixin.qq.com/cgi-bin/groups/create?access_token= "+ Access_tokento; string postData =" {\ "group \": {\ "name \": \ "" + this.txt groupsName. value. toString (). trim () + "\"} "; res = wxs. getPage (posturl, postData); ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('Created successfully! If not displayed, log out and log on again! '); Location = 'wxgroupmanagelist. aspx'; ", true );}

Modify group name page

Front-end code:

<! DOCTYPE html> 

Background code:

Protected void Page_Load (object sender, EventArgs e) {if (! Page. IsPostBack) {if (Request. QueryString ["id"]! = Null) {string group_id = Request. queryString ["id"]. toString (); string group_name = Request. queryString ["name"]. toString (); this.txt GroupId. text = group_id.ToString (); this.txt GroupName. text = group_name.ToString (); this.txt GroupName. focus ();}}} /// <summary> /// set /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> protected void LinkBtnSet_Click (object sender, even TArgs e) {if(String.IsNullOrWhiteSpace(this.txt GroupName. text. toString (). trim () {ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('cannot be blank! '); ", True); this.txt GroupName. focus (); return;} if (this.txt GroupName. text. toString (). trim (). length> 30) {ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('group name should be within 30 characters! '); ", True); this.txt GroupName. focus (); return;} WeiXinServer wxs = new WeiXinServer (); string res = ""; // read accesstoken string Access_token = Cache ["Access_token"] as string; if (Access_token = null) {// if it is null, obtain Access_token = wxs again. getAccessToken (); // set the cached data to expire after 7000 seconds. insert ("Access_token", Access_token, null, DateTime. now. addSeconds (7000), System. web. caching. cache. noSlidingExpiration);} string Access_tokento = Access_token.Substring (17, Access_token.Length-37); string posturl =" https://api.weixin.qq.com/cgi-bin/groups/update?access_token= "+ Access_tokento; // example of POST data: {" group ": {" id ": 108," name ": "test2_modify2" }}// string postData = "{\" group \ ": {\" name \ ": \" "+ this.txt groupsName. value. toString (). trim () + "\"} "; string postData =" {\ "group \": {\ "id \": \ "" + txtGroupId. text. toString () + "\", \ "name \": \ "" define this.txt GroupName. text. toString () + "\"} "; res = wxs. getPage (posturl, postData); // reference Newtonsoft before use. json. dll file JObject jsonObj = JObject. parse (res); // obtain the correct returned results | true | false, string isright = jsonObj ["errcode"]. toString (); // 0 string istrueorfalse = jsonObj ["errmsg"]. toString (); // OK if (isright. equals ("0") & istrueorfalse. equals ("OK") {// after the modification is successful, refresh the parent form and close ScriptManager on this page. registerClientScriptBlock (this. page, this. getType (), "", "alert ('modified successfully! If it is not properly displayed, It is a cache problem. Please log on again! '); Window. opener. location. reload (); this. close (); ", true) ;}else {ScriptManager. registerClientScriptBlock (this. page, this. getType (), "", "alert ('modification failed! '); This. close (); ", true );}}

The above is all the core code of user group management. It is for your reference only and I hope it will be helpful for your learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.