01 Jan 0001

Share


layout: post title: 知识死角,name相同的html element comments: true date: 2009-07-17 tags: - ASP.NET - 前端开发 - Asp.net

- C#

今天在帮同事调程序时发现了这个死角
现象如下,没有任何输入的textarea提交到服务端后得到的值总是”,”
找了半天发现是因为页面上两个textarea的name相同,以前只知道页面上radio的name相同则是一个组,name相同的
的checkbox提交后是以逗号分隔值的,于是做了下面代码的试验,发现任何name相同的element提交到服务端都会得到逗号分隔的值,以前还不知道这个,汗

<[email protected]=“C#“ %>

<script runat=“server” languag=“c#”>
    string textboxValue = string.Empty;
    string radioValue = string.Empty;
    string checkboxValue = string.Empty;
    string selectValue = string.Empty;
    string textareaValue = string.Empty;
    void Page_Load(object obj , EventArgs args)
    {
        placeHolder.Visible = false;
        System.Web.HttpContext ctx = System.Web.HttpContext.Current;
        if(ctx.Request.HttpMethod == ”POST”)
        {
            placeHolder.Visible = true;
            textboxValue = ctx.Request.Form[“textbox”];
            radioValue = ctx.Request.Form[“radio”];
            checkboxValue = ctx.Request.Form[“checkbox”];
            selectValue = ctx.Request.Form[“select”];
            textareaValue = ctx.Request.Form[“textarea”];
        }
    }
</script>

<!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=“http://www.w3.org/1999/xhtml" >
<head>
<style>
body, h1, h2, h3, h4, h5, h6, hr, p, ul, ol, li,pre,fieldset, lengend, button, input, textarea,{
    margin: 0;
    padding: 0;
}
body,button, input, select, textarea {
    font: 13px/1 Tahoma, Helvetica, Arial, ”\5b8b\4f53”, sans-serif;
}

#page{
    padding:10px 30px 10px 30px;
}
#s{
color:red;
}
input,select,textarea{
    clear:both;
    float:left;
    margin:5px;
}

pre {
    overflow: auto;
    border: 1px dotted #281;
    background-color: #eee;
    padding: 2px 20px 20px 10px;
}
fieldset{
    line-height:200%;
}

</style>
</head>
<body>

<div id=“page”>

<div id=“placeHolder” runat=“server”>    
            <string id=“s”>Post Data:</string>

<pre style=“max-height:30px;”>
textboxvalue:<%= textboxValue%>
radioValue:<%= radioValue%>
checkboxValue:<%= checkboxValue%>
selectValue:<%=selectValue%>
textareaValue:<%= textareaValue%></pre>

</div>

<form id=“aspnetForm” method=“post”>
                <fieldset>       
                  <legend>Test</legend>              
                <input type=“text” name=“textbox” value=“a”/> 
                <input type=“text” name=“textbox” value=“a”/>
                <input type=“text” name=“textbox” value=“a”/>
                <input type=“radio” name=“radio” value=“a” />
                <input type=“radio” name=“radio” value=“a” />
                <input type=“radio” name=“radio” value=“a” checked=“checked” />
                <input type=“checkbox” name=“checkbox” value=“a” checked=“checked” />
                <input type=“checkbox” name=“checkbox” value=“a” checked=“checked” />
                <input type=“checkbox” name=“checkbox” value=“a” checked=“checked” />
                <select name=“select”>
                                   <option value=“a”>a</option>
                </select>
                <select name=“select”>
                                   <option value=“a”>a</option>
                </select>
                <select name=“select”>
                                   <option value=“a”>a</option>
                </select>
                <textarea name=“textarea”>a</textarea>
                <textarea name=“textarea”>a</textarea>
                <textarea name=“textarea”>a</textarea>
                <input type=“submit” value=“Do PostBack” />
            </fieldset>
            </form>
    </div>

</body>
</html>

comments powered by Disqus