亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > ASP > 正文

Hiding/Manipulating Databound Items(轉載www.aspallia

2024-05-04 11:06:33
字體:
來源:轉載
供稿:網友
asp.net offers a powerful way to render information from a database or xml file: databinding. however, sometimes you need to be able to perform the databinding for almost all of the items in the data source. or perhaps you have some special formatting or secondary data that you need to use with each row in the data source, so that you need a little finer control over how the data is bound to the control. in these cases, you will typically need to handle the onitemdatabound event, which is fired for each item as it is bound to your control. this gives you a lot of flexibility, but unfortunately the code is a little bit hairy. hopefully, having an example will help! first of all, let me explain that this example is taken from a live application: my regular expression library over at my asp.net training website, aspsmith.com. you can see it in action by clicking here. you'll see what it is doing in a moment.

for my regular expression library, i had several things i needed to adjust beyond the default databinding that my datagrid provided "out-of-the-box". firstly, i had a user_id field in my data that i wanted to convert to a link to a user's email. next, i wanted to limit how much of the description field was displayed on the search results page, to avoid causing the page to get excessively long (since the description field is a text field in my database, and could therefore be many megabytes long). lastly, i had an edit link that allowed the owner of the regular expression to edit it, but i didn't want that to be visible except if the current user was the owner of the expression. let's see how this was done. first, let's take a look at my (rather lengthy) datagrid declaration. the important part is listed in red.

default.aspx excerpt <asp:datagrid id="dgregexp" runat="server" autogeneratecolumns="false" bordercolor="black" borderwidth="1" style="margin-left:20px;" pagesize="5" allowpaging="true" allowcustompaging="true" onpageindexchanged="dgregexp_pageindexchanged" onitemdatabound="dgregexp_itemdatabound" gridlines="horizontal" pagerstyle-mode="numericpages" pagerstyle-horizontalalign="center" pagerstyle-position="topandbottom" pagerstyle-width="100%" headerstyle-backcolor="#cc0000" headerstyle-font-bold="true" headerstyle-font-name="verdana" headerstyle-font-size="9pt" headerstyle-forecolor="white" itemstyle-font-name="arial" itemstyle-font-size="8pt" alternatingitemstyle-backcolor="#dddddd">  

this event, onitemdatabound, is supported by any control that supports data binding. you can use it with datagrids, datalists, repeaters, etc. in this case, i have mapped mine to the "dgregexp_itemdatabound" event handler, which we'll take a look at now:

default.aspx excerpt protected void dgregexp_itemdatabound(object sender, datagriditemeventargs e)
{
// for items and alternatingitems,
// convert userid to email link
// truncate description
// hide edit link if not owner
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
trace.write("itemdatabound",e.item.dataitem.gettype().tostring());

int user_id = int32.parse(((system.data.common.dbdatarecord)e.item.dataitem)["user_id"].tostring());
trace.write("itemdatabound", "user_id: " + user_id.tostring());

aspalliance.dal.userdetails objuser = aspalliance.dal.user.getuserdetails(user_id);
((system.web.ui.webcontrols.hyperlink)e.item.findcontrol("myuser")).text =
objuser.first_name + " " + objuser.last_name + " (" + objuser.email + ")";
((system.web.ui.webcontrols.hyperlink)e.item.findcontrol("myuser")).navigateurl = "mailto:" + objuser.email;
trace.write("itemdatabound", "myuser.text: " + ((system.web.ui.webcontrols.hyperlink)e.item.findcontrol("myuser")).text);

string desc = ((system.data.common.dbdatarecord)e.item.dataitem)["description"].tostring();
if(desc.length > 100)
{desc = desc.substring(0,99);
desc += "...";
}
((system.web.ui.webcontrols.label)e.item.findcontrol("description")).text = desc;
aspalliance.dal.security sec = new aspalliance.dal.security(this.request);
if((sec.user_id == 0) || (sec.user_id != user_id) || (!sec.isauthenticated))
{
((system.web.ui.htmlcontrols.htmltablecell)e.item.findcontrol("edittd")).visible = false;
}}}}


ok, now that's what i call an example! none of that wussy little "just for demonstration" three lines of code stuff for us. no, we're going to go all out and show you something big and ugly that actually sits on a production site. but don't fear, it will all be clear to you in a moment, if it isn't already (even if you only know vb). let's break this down.

the first 6 lines declare our method and throw in some comments. as i said, i basically have three things i want to do here:

convert userid to an email link
truncate the description field
hide the edit link if current user is not the owner
the only thing you really need to watch here is to make sure that your second parameter's type is correct for the control you are using. this is pretty self-explanatory, but if you can't figure it out, you can always look at the definition of the particular control you are using in the class browser. for you vb.net users, just convert "http://" to a single quote, get rid of the { and switch the parameters to the type is following the name and has "as" in front of it.

next, we need to make sure we're dealing with the correct item type. since this event is raised for every item in the bound control, including items, alternatingitems, separators, headers, footers, etc. ( complete list), we need to specify which kinds of items we are concerned with. in this case, we just want to deal with the main section of the control, so we check to make sure the item in question is either an item or an alternatingitem. this is handled by the if statement. we get the current item from our input parameter, e, and compare it to the itemtypes we are concerned with. for you vb guys, || means "or".

note: i neglected to use alternating items when i first wrote this application, so the user's email was displayed for every other item, but the user's id was displayed for the others!

ok, now i've got some tracing in place to help me with debugging. this just outputs the type of the current item, and lets me verify that it is in fact either an item or an alternatingitem. you can leave this out of your implementation.

next i grab the user_id. this is one ugly piece of code. let me repeat it here and go through it piece by piece:
int user_id = int32.parse( ((system.data.common.dbdatarecord)e.item.dataitem)["user_id"].tostring()); let's start with the innermost parentheses, in red. this is c#'s method of type casting, and is necessary to convert the current dataitem to the dbdatarecord type. the orange set of parentheses completes this operation. for all intents and purposes, the contents of the orange parentheses are considered to be a dbdatarecord. moving on to the green, this allows us to then reference the "user_id" element of this record, using c#'s array/collection syntax (in vb this would use () instead of []), and convert the contents to a string, because that is what int32.parse expects. finally moving out to the black, int32.parse converts a string into an int. the results of this conversion are then stored in my user_id variable of type int. on the next line i have some more diagnostic code to output the user_id to the trace log.

ok, so now we have a user_id. the next chunk of code uses some custom controls that i wrote to handle my users. the control is modelled after the ones found in the ibuyspy application. in this case, i userdetails class that holds my user's name and email address by calling the getuserdetails method of my user class. the next line is another hairy one, though:
( (system.web.ui.webcontrols.hyperlink)e.item.findcontrol("myuser")).text = objuser.first_name + " " + objuser.last_name + " (" + objuser.email + ")";
again, starting from the middle most parens, we have another typecasting operation being done. the red code is used to convert the orange code into a hyperlink. the orange code is used to find the control whose id is "myuser" within the current item. in my template for my column in my datagrid, i have an <asp:hyperlink id="myuser"/> tag that this code refers to. the rest of this block of code sets the text of this hyperlink to the user's name and email address. the hyperlink tag looks like this in my datagrid: <asp:hyperlink id="myuser" runat="server"> <%# databinder.eval(container.dataitem, "user_id") %> </asp:hyperlink>  


by now this typecasting is getting to be old hat. the next line pretty much does the same stuff as the previous line, but in this case we're setting the navigateurl property of our hyperlink to "mailto:" and the user's email address. once again this is followed by some more diagnostic tracing.
((system.web.ui.webcontrols.hyperlink)e.item.findcontrol("myuser")).navigateurl = "mailto:" + objuser.email;

that's it for the email. task 1 is complete. now we want to truncate the description if it is too long. we do this using similar techniques. first, we grab the "description" from the current dataitem after converting it to a dbdatarecord type. then we convert it to a string and assign it to a variable, desc (all in one line). next, we check to see if its length is more than 100, and if it is, we convert it to just the left 100 characters (using substring -- in vb we would have used left()) and tack on a "..." to the end to show the users that there is more text. finally, we use the findcontrol syntax once more to locate the description label control within our template, and set its text property to the result.

that's 2 tasks down. one to go. this is the most commonly asked for feature of datagrids and datalists: how to hide a particular row or item within a databound list. in this case, i use my custom security control to determine if the user is the owner of the current regular expression. if they aren't, or if they aren't logged in and authenticated, then i set the table cell holding my edit tag's visible property to false. in order to do this, i had to make my table cell an htmlcontrol by adding runat="server" to it and giving it an id. i named the table cell "edittd", and i can use findcontrol to locate the cell within my item and access its properties. the cell itself is listed here: <td align="right" valign="top" id="edittd" runat="server">
[<a href='edit.aspx?regexp_id=<%# server.urlencode(databinder.eval(container.dataitem, "regexp_id").tostring()) %>'
title="click to edit.">edit</a>]
</td>  
that's all there is too it. for most people, they want to know how to hide something if its value is null. just do the same thing i'm doing here, but replace the if statement that is referring to security with one that checks the value of the variable. you will probably need to use the (system.data.common.dbdatarecord)e.item.dataitem) syntax described earlier to access your data and check it for null, but that's all there is to it.

summary
manipulating or hiding individual elements of a databound list or grid is not very difficult once you know a few tricks. however, the code is pretty darned ugly. the code on this page works, and that is the best thing that can be said of any example. you can see it in action at aspsmith regular expression library. if you have trouble getting this to work with your own, i suggest that you first compare you code with mine, and if that doesn't yield any insight, ask the asp.net data experts on the aspng data listserv. be sure to include your failing code in your ques



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美疯狂性受xxxxx另类| 欧洲中文字幕国产精品| 欧美成人免费在线视频| 欧美丰满片xxx777| 久久夜精品va视频免费观看| 爱福利视频一区| 97高清免费视频| www.久久久久| 久久久久免费视频| 日韩av免费看| 欧美高清激情视频| 热99精品里视频精品| 国产福利精品在线| 国产精品视频资源| 九九九热精品免费视频观看网站| 色av吧综合网| 97在线视频免费播放| 久久久久久一区二区三区| 欧美久久精品午夜青青大伊人| 日韩乱码在线视频| 亚洲热线99精品视频| 成人午夜激情免费视频| 日韩女优人人人人射在线视频| 国产精品爱久久久久久久| 91在线观看免费| 久久中文字幕在线视频| 揄拍成人国产精品视频| 午夜精品美女自拍福到在线| 亚洲最新中文字幕| 日韩av毛片网| 精品国产91久久久| 久久久亚洲福利精品午夜| 91超碰caoporn97人人| 日韩高清不卡av| 亚洲欧美自拍一区| 精品久久久999| 九九久久久久99精品| 午夜精品99久久免费| 97**国产露脸精品国产| 精品久久久一区二区| 精品国偷自产在线视频99| 中文字幕少妇一区二区三区| 国产亚洲精品va在线观看| 久久久免费电影| 久久福利视频网| 欧美另类极品videosbest最新版本| 欧美精品生活片| 久久影院在线观看| 亚洲欧美精品suv| 欧美成人一区二区三区电影| 欧美日韩中文字幕在线视频| 欧美另类暴力丝袜| 亚洲成人久久久久| 国外成人性视频| 亚洲综合在线中文字幕| 久久精品国产成人| 国产亚洲精品高潮| 国产午夜精品免费一区二区三区| 欧美精品性视频| 精品国产一区二区三区四区在线观看| 亚洲人成电影在线| 欧美香蕉大胸在线视频观看| 亚洲国产精品va在线看黑人| 久久露脸国产精品| 亚洲国产中文字幕在线观看| 日韩精品有码在线观看| 国产精品人成电影在线观看| 一区二区三区国产在线观看| 亚洲精品www久久久久久广东| 亚洲精品国产精品国自产在线| 成人免费在线视频网站| 色小说视频一区| 亚洲韩国日本中文字幕| 成人国产精品一区二区| 色综合久久88| 亚洲女同性videos| 亚洲免费电影一区| 亚洲91精品在线观看| 国产精品国产三级国产aⅴ9色| 91久久精品久久国产性色也91| zzjj国产精品一区二区| 国产精品视频26uuu| www.亚洲人.com| 欧美日韩人人澡狠狠躁视频| 亚洲国产精品久久精品怡红院| 成人天堂噜噜噜| 亚洲精品久久久久久久久| 欧美在线日韩在线| 久久国产精品免费视频| 亚洲国产另类 国产精品国产免费| 国产亚洲视频中文字幕视频| 日韩毛片在线看| 日韩在线视频网站| 成人免费在线视频网址| 日韩在线视频播放| 美女性感视频久久久| 国产精品吴梦梦| 欧美激情久久久| 欧美日韩在线一区| 日韩中文理论片| 国产精品网站视频| 亚洲黄色av女优在线观看| 欧美自拍大量在线观看| 欧美丝袜第一区| 久久精视频免费在线久久完整在线看| 深夜福利日韩在线看| 久久天堂电影网| 国产午夜精品视频免费不卡69堂| 亚洲性av在线| 国产精品入口尤物| 久久精品免费电影| 岛国av一区二区在线在线观看| 亚洲国产另类 国产精品国产免费| 国产美女精品免费电影| 国产午夜精品久久久| 亚洲精品视频在线观看视频| 亚洲欧美激情精品一区二区| 久久不射热爱视频精品| 国产精品专区h在线观看| 97久久久免费福利网址| 久久人人爽国产| 性欧美激情精品| 日韩在线观看成人| 欧美亚洲在线播放| 亚洲精品中文字幕女同| 久久精品国产亚洲| 欧美精品日韩www.p站| 欧美精品免费在线观看| 欧美性猛交xxxx久久久| 久久精品在线视频| 91在线高清视频| 欧美激情视频一区| 亚洲黄页网在线观看| 国产在线观看一区二区三区| 欧美成人精品一区二区| 日韩中文字幕免费| 国产精品专区h在线观看| 欧美人在线视频| 亚洲区免费影片| 性色av一区二区三区免费| 日韩中文在线不卡| 国产精品久久久久久久久影视| 亚洲香蕉伊综合在人在线视看| 欧美一区二区三区艳史| 久久精品在线播放| 97国产suv精品一区二区62| 欧美性猛交xxxx乱大交3| 国产精品激情av在线播放| 国内免费久久久久久久久久久| 俺也去精品视频在线观看| 最新的欧美黄色| 久久久久久国产三级电影| 亚洲欧美日韩精品久久| 亚洲国产成人一区| 成人黄色av免费在线观看| 精品福利视频导航| 久久99精品久久久久久琪琪| 欧美激情女人20p| 亚洲第一区在线观看| 九九九久久久久久| 91精品国产91久久久久久吃药| 色狠狠av一区二区三区香蕉蜜桃| 国产精品久久久久久久美男| 在线国产精品播放|