首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

CommandName有关问题

2013-12-10 
CommandName问题form idform1 runatserverdiv asp:ListViewIDlstMovieCategoriesDataSourc

CommandName问题
<form id="form1" runat="server">
    <div>
 >
        <asp:ListView 
            ID="lstMovieCategories"
            DataSourceId="srcMovieCategory"
            DataKeyNames="Id"   
            runat="server">
            <LayoutTemplate>
                <div class="categoryContainer">
                <asp:PlaceHolder
                    id="itemPlaceholder"
                    Runat="server" />
                </div>
            </LayoutTemplate>
            <ItemTemplate>
                <div>
                <asp:LinkButton
                    id="lnkSelect"
                    Text='<%# Eval("Name") %>'
                    CommandName="Select"
                    Runat="server" />
                </div>
            </ItemTemplate>
            
   
            <SelectedItemTemplate>
               
                <div id="selected">
                <%# Eval("Name") %>
                <i>yang</i>
                </div>
            </SelectedItemTemplate>
        </asp:ListView>
    
        <br style="clear:both" /><br />
        
        <asp:ListView 
            ID="lstMovies"
            DataSourceId="srcMovies"
            runat="server">
            <LayoutTemplate>
                <ol>
                <asp:PlaceHolder 
                    id="itemPlaceholder"
                    runat="server" />
                </ol>
            </LayoutTemplate>
            <ItemTemplate>
                <li><%# Eval("Title") %></li>
            </ItemTemplate>
        </asp:ListView>
    
        <asp:SqlDataSource
            id="srcMovieCategory"
            SelectCommand="SELECT Id, Name FROM MovieCategory"
            ConnectionString='<%$ ConnectionStrings:con %>'
            Runat="server" />



        <asp:SqlDataSource
            id="srcMovies"
            SelectCommand="SELECT Title FROM Movie
                WHERE CategoryId=@CategoryId"
            ConnectionString='<%$ ConnectionStrings:con %>'
            Runat="server">
            <SelectParameters>
                <asp:ControlParameter
                    
                    Name="CategoryId" 
                    ControlID="lstMovieCategories" />
            </SelectParameters>
        </asp:SqlDataSource>    
    </div>
    </form>


这个CommandName有什么作用
还有最后那个SelectParameters怎么没有设置PropertyName属性
CategoryId这个参数的值又从哪里来

[解决办法]
CommandName用于触发Command事件后判断该做什么操作。
如果设置update,insert,select的话,可以触发默认的对应操作。
如果是自定义按钮,或其他CommandName的话。可以在Command事件中判断e.CommandName来做不同的操作。

SelectParameters的设置:


            <SelectParameters>
                <asp:ControlParameter ControlID="控件名称" Name="SQL中的参数名" PropertyName="控件的属性" 
                    Type="String" />
            </SelectParameters>

[解决办法]
<asp:LinkButton CommandName="select" CommandArgument='<%#Eval("ID") %>'

加一个CommandArgument='<%#Eval("ID") %>'
<asp:LinkButton CommandName="delete" CommandArgument='<%#Eval("ID") %>'[/code]
加一个[color=#FF0000]CommandArgument='<%#Eval("ID") %>'

当你点击这个LinkButton时,先判断if(e.CommandName=="select")然后就能string id=e.CommandArgument.ToString();得到你点击这一行的主键ID

热点排行